0

So I am trying to append a logo in a RichText editor immediately after the user hits "Go" and before the new page loads.

$('#Go').on('click', function (e) {
    e.preventDefault();
    $.get(url, function(data) {
        var html = '<img src="' + data + '" width=100px height=100px></img>';
        $('.EditorFrame').contents().find('body').append(html);
    });
});

Just as I hit "Go", the image briefly appears in the editor before the new page is loaded. The new page - which shows nothing but the result of our editing comes out blank! I have tried this with plain text as well - same story.

I have tried hard to debug. For instance, putting:

console.log($('.EditorFrame').contents().find('img'));

immediately after the append line, does print out the img tag!

Interestingly, when I copy/paste the same code in the console and then press "Go", everything works as normal and I can see the image, text whatever I put in the editor.

I am using the latest Google Chrome for my labors.

5
  • does it work if you change ></img> to />. Images are self-closing tags Commented May 3, 2014 at 22:13
  • "Just as I hit "Go", the image briefly appears in the editor before the new page is loaded." What do you mean "new page is loaded?" Any changes made to the current DOM will be lost once a new page is loaded - including during any postbacks. It may be worth ensuring that your button has "type='button'" specified on it, to make sure you're not accidentally posting your form back to the server. Commented May 3, 2014 at 22:16
  • 1
    If #Go is a type="submit" button, then the <form>'s 'submit' event is what needs e.preventDefault(). The 'click' event alone isn't what causes the navigation. Commented May 3, 2014 at 22:16
  • ...you might remove the action from your form (assuming as others have said that this button is in a form) Commented May 3, 2014 at 22:17
  • Thanks all. The fundamental problem here is that the same code is running perfectly fine in the console and yet not in the script. In this, I believe @rob-m is pointing in the right direction. Commented May 4, 2014 at 5:50

1 Answer 1

2

You are racing your async get method and next page load. You probably should return false in your click handler and go to the next page (if that's what you desire) in the success callback of your get request:

$('#Go').on('click', function (e) {
    e.preventDefault();
    $.get(url, function(data) {
        var html = '<img src="' + data + '" width="100" height="100"></img>';
        $('.EditorFrame').contents().find('body').append(html);
        // go to next page here
    });
    return false;
});

Also, width and height attributes should not have "px" and should just be numeric values

Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful. One little problem though. In order to go to the next page, I am having to trigger the click event. But before that, I have to 'off' the handler least we get stuck in the handler trigger loop --- $('#Go').off('click').click(); Interestingly, the click event does not fire here. I am having to manually click the #Go button. I would really appreciate if you could bail me out again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.