AJAX 调用后 JavaScript 不加载
好吧,当我加载页面时,会解析以下 HTML 代码:
<div id="clip-wrapper">
<script type="text/javascript">
alert("bla");
</script>
</div>
这显然会导致出现“bla”警报,在我的 AJAX 调用之后,解析的 HTML 代码如下所示:
<div id="clip-wrapper">
<script type="text/javascript">
alert("ajaxbla");
</script>
</div>
另一方面,这不会导致警报。为什么?我该如何解决这个问题?
Ok the following HTML code is parsed when I load a page:
<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:
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果有任何脚本通过 Ajax 调用加载到您的 dom 中,出于安全原因它们将不会被执行。
要执行脚本,您需要将其从响应中删除,并通过
eval
运行它。但理想情况下,您希望将脚本作为回调运行 strong> 当你的 ajax 请求完成时。如果您使用的是 jQuery,则这将是 ajax 调用的 success 事件,如果您在本机执行此操作,则这将是 xhr 对象的 readyStateChange 事件。
编辑
如果您确实想选择 eval 选项,您可以尝试如下操作:
If there are any scripts loaded into your dom from an Ajax call, they will not be executed for security reasons.
To get the script to execute, you'll need to strip it out of the response, and run it through
eval
Ideally though, you'll want to run your script as a callback for when your ajax request completes. If you're using jQuery, this would be the success event of your ajax call, and if you're doing it natively, it would be the readyStateChange event of your xhr object.
EDIT
If you really want to opt for the eval option, you could try something like this:
将代码放入
或使用 XMLHttp 对象的 readysatchange 事件。
编辑
如果您使用jquery而不是
在成功事件中转到此处,您可以编写代码,该代码在完成ajax后执行。
put you code in
or make use of readysatchange event of XMLHttp object.
Edit
if you are using jquery than go for
here in sucess event you can write your code which get executed once you are done with the ajax.
正文中
script
标记之间的任何代码仅在加载文档时才会执行。在ajax调用中,如果您使用的是jQuery,请使用
callback
或success:
或oncomplete:
来处理ajax响应。any code inside body in between
script
tag will executed only while loading the document.in ajax call,make use of
callback
orsuccess:
oroncomplete:
to handle the ajax response if you are using jQuery.你的意思是第二个 html 代码是作为 ajax 响应来的?您甚至将 html 插入到当前页面了吗?
类似的东西,
但即使使用
innerHTML
替换,我也不认为其中的script
会自动执行。浏览器将解析脚本
,但不会自动执行它。 (例如,如果您在 html 中有一个function
定义,则在 ajax 调用之后您将能够调用这些函数)另一种选择是使用
document.write
,它将用新的 html 替换整个页面内容,并且其中的script
标签将自动执行。希望这有帮助,
You mean the second html code is coming as an ajax response?? Did you even inserted the html to the current page?
Something like,
But even with
innerHTML
replacement I don't thing thescript
inside it will be auto executed. The browser will parse thescript
but won't auto execute it. (For eg, if you have afunction
definition in the html, after the ajax call you will be able to call those functions)Another option is to use
document.write
, it will replace the whole page content with the new html and thescript
tags in it will be auto executed.Hope this helps,