如何获取嵌入在通过jquery load()方法检索的内容中的脚本?

时间:2009-09-02 19:48:54

标签: javascript jquery ajax

我在S.O.上找到了其他几个问题。 (以及一般的网络)大致提出同样的问题,但答案似乎总是建议其他方法来构建代码,避免解决这个潜在问题的需要。

例如,许多人建议(并且我同意这是一个很好的建议)将代码放在jquery load方法的回调中,在调用页面而不是被调用页面上。但是我有一些可能出现在某些资源中的独特脚本,因此我不想为每次加载都这样做,也不一定知道这些脚本会是什么。

这是一个测试设置,用于演示我正在尝试做什么。 简短摘要是,当我从partial.htm加载main.htm时,其脚本无法启动。

main.htm中:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>main file</title>
</head>
<body>
<ul id="links">
    <li><a href="somepage1.php">some page1</a></li>
    <li><a href="somepage2.aspx">some page 2</a></li>
    <li><a href="partial.htm">some other partial page</a></li>
</ul>
<div id="panel" style="display:none; padding:20px; background-color:#CCC;">
LOADED CONTENT WILL GO HERE
</div>
<script type="text/javascript" src="/path/to/jquery-1.3.2.min.js"> </script>
<script type="text/javascript">
$(function(){
    $('#links a').click(function() {
        var $panel = $('#panel');
        $panel.show();
        $panel.html('Please wait...');
        var href = $(this).attr('href');
        $('#panel').load(href + ' #content');
        return false;
    });
});
</script>
</body>
</html>

好的,此页面上的功能非常简单。想象一下,还有更多的链接,其中一些可能需要编写脚本,而其他链接则不需要。

这是 partial.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>partial file</title>
</head>
<body>
<div id="content">
    <p>Hey, I am the partial file!</p>
    <script type="text/javascript">
    alert('I am some JS in the partial file! But sadly I do not execute...');
    </script>
</div>
<div>
    I am some other content on the page that won't be included by jquery.load()...
</div>
</body>
</html>

请注意,partial.htm中的脚本无法触发。所以,我的问题仍然是:如何解决这个问题,排除任何答案,告诉我把它放在.load()方法的回调中。 (这可能是因为我可能不知道这些部分页面可能包含或需要哪些脚本!)

谢谢!

<小时/> 更新#1:

我认为一个可接受的答案就是“你做不到”。但是,我想知道这是否确实如此。我还没有找到任何正式陈述的东西。

此外,当我之后使用firebug检查panel区域时,根本没有脚本元素。就像它被load解析出来一样。

<小时/> 更新#2:

当使用选择器作为href的一部分时,我已将其缩小为的问题。加载整个"somepage.html"将执行脚本,但加载"somepage.html #someregion"则不会。

$panel.load('somepage.html');              // my script fires!
$panel.load('somepage.html #someregion');  // script does not fire

我将尝试寻找为什么在jquery源中可能出现这种情况......

2 个答案:

答案 0 :(得分:4)

好吧,这似乎是设计的。显然让IE变得快乐,我们其余的人都受苦了。这是jquery源代码中的相关代码:

// See if a selector was specified
self.html( selector ?
    // Create a dummy div to hold the results
    jQuery("<div/>")

        // inject the contents of the document in, removing the scripts
        // to avoid any 'Permission Denied' errors in IE
        .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))

        // Locate the specified elements
        .find(selector) :

    // If not, just inject the full result
    res.responseText );

我想知道,我是否可以修改jquery源代码以便以其他方式包含它们使IE变得快乐,而不仅仅是剥离脚本?我还没有在网上找到其他任何讨论此事的内容,我敢肯定我不是唯一一个被这个难过的人?

答案 1 :(得分:0)

我之前遇到的问题是IE没有运行不包含defer属性的注入<script>。此讨论主题包含有关该主题的一些有用信息:innerHTML and SCRIPT tag

相关问题