简体   繁体   中英

Is there a other, better way to call this js, which is coming from ajax?

I would like to ask about eval() function, I have working code where I use this function, but I saw that is not good practice to be used. so I was wondering is there any other way to do that: I have response from ajax, this response is a web page, which contains a bit of js code inside. So when I parse this page in a div jquery removes all js, that's why I have id attribute of my which is inside that page so i can access that code and than I execute with eval. I'll put some code for example:

success:function(data){
 $('title').html($(data).filter('title').html()); 
 var js = $(data).filter('#script').html(); 
 var page = $(data).filter('#content').html(); 
 $('#content').html(page); 
 eval(js);
}

So actually my question is there a other, better way to call this js, which is coming from ajax?

You don't need to do anything special to run the script. As long as it's in script tags it will run when it's appended to the page with .html() with the rest of the content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM