AJAX 调用后 Javascript 不加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8440642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:27:43  来源:igfitidea点击:

Javascript doesn't load after AJAX call

javascriptajax

提问by pbond

Ok the following HTML code is parsed when I load a page:

好的,当我加载页面时会解析以下 HTML 代码:

<div id="clip-wrapper"> 
    <script type="text/javascript"> 
        alert("bla");
    </script> 
</div>

This obviously leads to an alert saying "bla", after my AJAX call, the parsed HTML code looks like this:

这显然会导致警告说“bla”,在我的 AJAX 调用之后,解析的 HTML 代码如下所示:

<div id="clip-wrapper">
<script type="text/javascript">
    alert("ajaxbla");
</script>
</div>

This on the other hand, doesn't result in an alert. Why? And how do I fix this?

另一方面,这不会导致警报。为什么?我该如何解决这个问题?

采纳答案by Adam Rackis

If there are any scripts loaded into your dom from an Ajax call, they will notbe executed for security reasons.

如果有任何脚本从 Ajax 调用加载到您的 dom 中,出于安全原因,它们将不会被执行。

To get the script to execute, you'll need to strip it out of the response, and run it through eval

要执行脚本,您需要将其从响应中剥离,然后运行它 eval

Ideally though, you'll want to run your script as a callbackfor when your ajax request completes. If you're using jQuery, this would be the successevent of your ajax call, and if you're doing it natively, it would be the readyStateChange event of your xhr object.

理想情况下,您希望在 ajax 请求完成时将脚本作为回调运行。如果您使用的是 jQuery,这将是您的 ajax 调用的成功事件,如果您在本地进行,它将是您的 xhr 对象的 readyStateChange 事件。

EDIT

编辑

If you really want to opt for the eval option, you could try something like this:

如果你真的想选择 eval 选项,你可以尝试这样的事情:

document.getElementById(contentDiv).innerHTML = xmlHttp.responseText;
eval(document.getElementById("clip-wrapper").innerHTML);

回答by dku.rajkumar

any code inside body in between scripttag will executed only while loading the document.

script标记之间的正文中的任何代码只会在加载文档时执行。

in ajax call,make use of callbackor success:or oncomplete:to handle the ajax response if you are using jQuery.

在 ajax 调用中,如果您使用的是 jQuery ,请使用callbacksuccess:oncomplete:来处理 ajax 响应。

回答by Pranay Rana

put you code in

把你的代码放进去

$(document).ready(function()
{
  alert(msg);
});

or make use of readysatchange event of XMLHttp object.

或者利用 XMLHttp 对象的 readysatchange 事件。

XMLHttpobject.readyStateChange( furnction() { alert('msg'); } );

Edit

编辑

if you are using jquery than go for

如果你使用 jquery 比去

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

here in sucess event you can write your code which get executed once you are done with the ajax.

在这里,在成功事件中,您可以编写代码,一旦您完成了 ajax,就会执行这些代码。

回答by Jim Jose

You mean the second html code is coming as an ajax response?? Did you even inserted the html to the current page?

你的意思是第二个 html 代码是作为 ajax 响应出现的??您是否甚至将 html 插入到当前页面?

Something like,

就像是,

document.body.innerHTML = ajax_response;

But even with innerHTMLreplacement I don't thing the scriptinside it will be auto executed. The browser will parse the scriptbut won't auto execute it. (For eg, if you have a functiondefinition in the html, after the ajax call you will be able to call those functions)

但即使innerHTML更换我也不知道script它会自动执行。浏览器将解析script但不会自动执行它。(例如,如果您function在 html 中有定义,则在 ajax 调用后您将能够调用这些函数)

Another option is to use document.write, it will replace the whole page content with the new html and the scripttags in it will be auto executed.

另一种选择是使用document.write,它将用新的 html 替换整个页面内容,并且其中的script标签将自动执行。

document.write(ajax_response);

Hope this helps,

希望这可以帮助,