运行 AJAX 调用的 javascript 代码
我的网站使用pushState来加载页面。我有一个问题,我想在其中一个页面上使用 javascript,但不能,因为它使用 AJAX 加载所有内容。那我该怎么办?有人告诉我一些有关“parseScript”的信息,但我找不到足够的信息。
--示例--
我使用 AJAX 加载 在我的页面上有这样的脚本:
<script type="text/javascript">
function go(){
alert('1');
}
</script>
<a href="javascript:void();" onClick="go();">GO!!!</a>
什么也没有发生。
--编辑--
如果我打开 Google Chrome 的调试器: “未捕获的引用错误:go 未定义” 和 <脚本>标签在哪里找不到
My site uses pushState to load pages. I have one issue, I want to use javascript on one of the pages but can't because it loads everything with AJAX. So what do I do? I've been told something about "parseScript" but I can't find enough information on it.
--Example--
I load using AJAX
On my page I have this script:
<script type="text/javascript">
function go(){
alert('1');
}
</script>
<a href="javascript:void();" onClick="go();">GO!!!</a>
Nothing happens.
--Edit--
If I open up Google Chrome's debugger:
"Uncaught ReferenceError: go is not defined"
And the <script> tag is no where to be found
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
浏览器似乎无法解析通过
targetElement.innerHTML
添加到文档的元素内容。这可能就是你遇到的情况。
最好的解决方案是使用像 jQuery 这样经过良好测试的框架来解决此类问题。他们已经弄清楚如何安全、正确地将脚本注入 DOM 中。除非您绝对无法为库腾出带宽,否则重新发明轮子是没有意义的。
解决此问题的一种方法是在 Ajax 响应中将 JavaScript 与 HTML 分开,方法是发出两个请求(可能较慢)或在 JSON 对象中构建 JavaScript 和 HTML(可能更难维护)。
下面是一个示例:
服务器上的文档
ajax.json
如下所示:如果您选择此路线,则必须:
MyApp.foo = function (){ .. };
,或window.foo = function (){ ... };
。这是因为
eval
在当前作用域中执行,因此您的函数定义继承该作用域并且不会全局可用。在我的示例中,我选择了后一个选项,因为它只是一个简单的示例,但您应该知道为什么这是必要的。请务必阅读JavaScript 的 eval() 什么时候不是邪恶的? 如果您决定自己实现这一点。
Browsers don't seem to parse
<script>
element content that's added to the document viatargetElement.innerHTML
. That's probably what you're running into.The best solution is to use a well-tested framework like jQuery for solving problems like this. They've already figured out how to safely and correctly inject scripts into the DOM. There's no sense re-inventing the wheel unless you absolutely can't spare the bandwidth for the library.
One way you might fix this is by separating the JavaScript from the HTML in the Ajax response, either by issuing two requests (probably slower) or by structuring your JavaScript and HTML within a JSON object (probably harder to maintain).
Here's an example:
The document
ajax.json
on the server looks like this:If you choose this route, you must either:
MyApp.foo = function (){ ... };
, orwindow.foo = function (){ ... };
.This is because
eval
executes in the current scope, so your function definitions inherit that scope and won't be globally available. In my example, I chose the latter option since it's just a trivial example, but you should be aware of why this is necessary.Please make sure to read When is JavaScript's eval() not evil? if you decide to implement this yourself.
我认为更详细地了解如何进行 Ajax 调用和加载内容会很有帮助。也就是说,有一些值得注意的事情:
I think it would be helpful to have a little more detail as to how the Ajax call is made and the content is loaded. That said, a few things of note:
如果我要这样做,我会使用 jquery 的 load 调用。
它负责放置 ajax 调用,并解析脚本/无脚本元素的标签。
如果您不想使用 jquery,我建议您上网查找 jquery load 方法的作用,并为您的 ajax 调用实现与事件处理程序相同的方法。
If I were to do this I would use jquery's load call.
That takes care of putting an ajax call ,and parsing tags for script/no-script elements.
IF you dont wanna use jquery, I would suggest you go online and find what the jquery load method does and implement the same as an event handler for your ajax call.