2

I'm trying to load a page using the load() function, the problem is that javascript code on that page is being executed when loading. I use this:

$('#itemid').load('thepage.php #selector', function() {
     MY CODE HERE
});

how can i prevent the javascript code from being executed and load only the HTML part that i want?

2 Answers 2

3

Use .get() or .post() and process what you get back. Pull the script tags out of your returned code before you append it to the page, or just pull out the code you want:

$.post('thepage.php', {
    data: myData
}, function(data) {
    var myHTML = $(data).not('script');
    $('#itemid').html(myHTML);
});

Or:

$.post('thepage.php', {
    data: myData
}, function(data) {
    var myHTML = $(data).filter('#selector');
    $('#itemid').html(myHTML);
});

Demo: http://jsfiddle.net/jtbowden/wpNBM/

Note: As you mentioned, using a selector with load should accomplish the same thing, as you see in the example. So, if it isn't working this way, something else is going on.

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

3 Comments

i think that this is exactly what i need, i'll try it right now and report back the results. I forgot to mention that i'm not uusing jQuery exactly, but i'm using the Zepto script (that should work in same way for that functions). I'm thinking that it's a bug, cause reading the jQuery documentation for the load() function i've readed that scripts are executed ONLY if load is used WITHOUT using a selector from the loaded page... but in Zepto it's not working that way
Yes, the selector should work, like in my jsfiddle example. I am not familiar with Zepto, but from what it sounds like the syntax is the same, but not all features are guaranteed to be the same.
From the Zepto docs: "Note that emulation of all features of jQuery is not a project goal". They are most concerned with being a small library. That means they give up some features to achieve that.
0

Not sure if I have understood the problem correctly, but you could remove the javascript and just have the html. I assume you want to js bindings to happen on the new page though. So when you load the new page, in the callback, you could call a function that applies the needed bindings.

function applyAfterAjax(){
  $(this).find('element').click(function(){alert('clicked');});
}
$('#itemid').load('thepage.php #selector',applyAfterAjax);

Comments

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.