最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove ajax loaded content and script - Stack Overflow

programmeradmin1浏览0评论

I dont know this may be duplicate but,

I use ajax in my website, now in response to that call i send some html and script.

Then I replace that data with some of my element exist in page.

Now, I want to make second ajax call so I have removed html content from loaded div. Then again ajax call return some other html code and script. But Some times it is conflict between previous script and newly arrived script.

EX. 1st response I replace html and script with some element: $('.ma_right_main').html(response.data);

then before 2nd call I remove content: $('.ma_right_main').html('');

2nd response I replace html and script with some element: $('.ma_right_main').html(response.data);

but after replace it 2nd time script returned from first time still there in execution. I want to remove it just like how I can remove html content.

I dont know this may be duplicate but,

I use ajax in my website, now in response to that call i send some html and script.

Then I replace that data with some of my element exist in page.

Now, I want to make second ajax call so I have removed html content from loaded div. Then again ajax call return some other html code and script. But Some times it is conflict between previous script and newly arrived script.

EX. 1st response I replace html and script with some element: $('.ma_right_main').html(response.data);

then before 2nd call I remove content: $('.ma_right_main').html('');

2nd response I replace html and script with some element: $('.ma_right_main').html(response.data);

but after replace it 2nd time script returned from first time still there in execution. I want to remove it just like how I can remove html content.

Share Improve this question edited Jul 20, 2012 at 9:30 thecodeparadox 87.1k22 gold badges141 silver badges164 bronze badges asked Jul 20, 2012 at 9:29 ParixitParixit 3,8553 gold badges39 silver badges63 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

I don't think once a browser has loaded a script that it can be removed (this doesn't mean you can't remove it from the dom, but it would still exist.) For example if you had a script with variable x, load the page so the script gets loaded, remove the script from the dom, x I think would still be defined.

There are ways around this though, if each dynamically loaded script was its own object, when "unloading" you could set the object to null. This is a solution I came across a while back when looking for a similar solution.

Found the reference I was referring to: https://stackoverflow./a/1346903/1475461

The scripts aren't naturally executed at all. (Setting .innerHTML will not execute scripts inside the HTML).

jQuery manually finds all the script elements in the html, removes them and evaluates them. So if you are using .html and don't want the scripts to execute, do not include scripts in the html.

And you cannot deactivate scripts, so you need to refactor your website with this in mind. Don't include new code in responses if they can conflict with previous ones. Use generic code that is always loaded.

发布评论

评论列表(0)

  1. 暂无评论