jQuery的.load()调用不会在加载的html文件执行的JavaScript [英] jQuery .load() call doesn't execute javascript in loaded html file

查看:138
本文介绍了jQuery的.load()调用不会在加载的html文件执行的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是与Safari浏览器只有一个问题。我试过4在Mac和3在Windows和我仍然没有运气。

This seems to be a problem related to Safari only. I've tried 4 on Mac and 3 on Windows and am still having no luck.

我试图加载一个外部HTML文件并嵌入执行JavaScript。

I'm trying to load an external HTML file and have the JavaScript that is embedded execute.

在code我想使用是这样的:

The code I'm trying to use is this:

$("#myBtn").click(function() {
    $("#myDiv").load("trackingCode.html");
});

跟踪code.html 看起来是这样的(现在简单了,但将扩大一旦/如果我得到这个工作):

trackingCode.html looks like this (simple now, but will expand once/if I get this working):

<html>
<head>
    <title>Tracking HTML File</title>
    <script language="javascript" type="text/javascript">
        alert("outside the jQuery ready");
        $(function() {
            alert("inside the jQuery ready");
        });
    </script>
</head>

<body>
</body>
</html>

我看到在IE中这两个警报消息(6和7)和Firefox(2及3)。但是,我不能够看到在Safari消息(最后的浏览器,我需要去关注 - 项目要求 - 请无火焰战争)。

I'm seeing both alert messages in IE (6 & 7) and Firefox (2 & 3). However, I am not able to see the messages in Safari (the last browser that I need to be concerned with - project requirements - please no flame wars).

为什么Safari浏览器是忽略了JavaScript中的跟踪code.html有什么想法文件?

Any thoughts on why Safari is ignoring the JavaScript in the trackingCode.html file?

最后,我希望能够通过JavaScript对象本跟踪code.html 文件被jQuery的准备调用中使用,但我倒要确保这是有可能在所有的浏览器之前,我这条路走。

Eventually I'd like to be able to pass JavaScript objects to this trackingCode.html file to be used within the jQuery ready call, but I'd like to make sure this is possible in all browsers before I go down that road.

推荐答案

正在加载整个HTML页面到您的分区,包括HTML,头部和身体标记。如果你做的负荷,只是有开脚本,关闭脚本和JavaScript code在HTML您加载怎么办?

You are loading an entire HTML page into your div, including the html, head and body tags. What happens if you do the load and just have the opening script, closing script, and JavaScript code in the HTML that you load?

下面是驱动程序页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>jQuery Load of Script</title>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("jquery", "1.3.2");
        </script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#myButton").click(function() {
                    $("#myDiv").load("trackingCode.html");
                });
             });
         </script>
    </head>
    <body>
        <button id="myButton">Click Me</button>
        <div id="myDiv"></div>
    </body>
</html>

下面是跟踪code.html的内容:

Here is the contents of trackingCode.html:

<script type="text/javascript">
    alert("Outside the jQuery ready");
    $(function() {
        alert("Inside the jQuery ready");
    });
 </script>

这在Safari 4为我工作。

This works for me in Safari 4.

更新:新增DOCTYPE和HTML的命名空间,以配合在我的测试环境中的code。测试与Firefox 3.6.13和例如code ++工程。

Update: Added DOCTYPE and html namespace to match the code on my test environment. Tested with Firefox 3.6.13 and example code works.

这篇关于jQuery的.load()调用不会在加载的html文件执行的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆