1

I have an ajax call which returns me following response:

<div class="content">
    <script language="JavaScript">  
        function validate(){
            alert('validate is working');
        }
    </script>
    <div class="container">
        <a href="#" onclick="javascript:validate();">my button</a>
    </div>
</div>

Now, i want to convert the response into html and append it to the page, which made the ajax call.

$.get('test.php', function(data) {
    $('#someTarget').append($(data).find('.content').html());
});

I have tried above code, but it seems this only append container div. Is there a way to append javascript code block into my existing page?

2
  • 1
    What you have should work given the correct selector, you missed the . in .find('.content'). How are you confirming that the script isn't being executed? Commented Nov 14, 2012 at 15:05
  • hey @KevinB, i've just misspelled .content here - i fixed it. I assume script is not executed as i can't see validate function in DOM - and there is no alert when i click 'my button'. Commented Nov 14, 2012 at 18:26

1 Answer 1

1

In your code are two problems:

$('#someTarget').append($(data).find('content').html());
  1. $(data) does parse and execute your script, before you inserted it into DOM

  2. assuming you meant $(data).find('.content').html() the html() returns the innerHTML of content, you only need $(data).

Try this:

<div class="content">
    <style type="text/javascript">  
        function validate(){
            alert('validate is working');
        }
    </style>
    <div class="container">
        <a href="#" onclick="javascript:validate();">my button</a>
    </div>
</div>

Now there is a trick: I'm using <style> to wrap scripts, that should be executed after interpreting and inserting into DOM.

$.get('test.php', function(data) {

    // parse HTML to DOM
    var $data = $(data);

    // take the scripts out
    var $inlineScripts = $('style[type="text/javascript"]', $data).remove();

    // Now append the DOM
    $('#someTarget').append($data);

    // And globalEval the scripts
    $inlineScripts.each(function () {
        $.globalEval(this.innerText);
    });
});

Okay for your simple validate function that gets called by a click handler, you currently don't need my trick for inline scripts, however you will need it, when you have active scripts, that should be executed after the HTML is parsed and inserted.

Update: If you don't want the '.content' to be inserted, don't use html() - because it returns a string, so you do parse the html twice. Use instead .append( $(".container", $(data)) ) or .append( $(data).find(".container") ).

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

2 Comments

Thanks for your answer. I'll try your suggestions - globalEval might be the thing i'm looking for. I'll let you know.
beware that $(data) parses and evaluates inline scripts. that is why i'm wrapping it into style and then do globalEval.

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.