如何获得脚本火灾嵌入在通过jQuery的load()方法检索到的内容? [英] How to get scripts to fire that are embedded in content retrieved via the jquery load() method?

查看:114
本文介绍了如何获得脚本火灾嵌入在通过jQuery的load()方法检索到的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了其他一些问题在这里SO (和网络一般),其大致问同样的问题,但答案似乎总是暗示的结构code等方式,避免需要解决这一潜在的问题。

I have found several other questions here on S.O. (and the web in general) that ask roughly this same question, but the answers always seem to suggest other ways of structuring code that avoid the need for addressing this underlying issue.

例如,很多人认为(和一个很好的建议,我同意),把你的code在jQuery的load方法的回调,调用页面,而不是所谓的页面上。但是我有可能会出现在某些资源独特的脚本,所以我也不想这样做,对于每一个载荷和我也不一定知道这些脚本会。

For example, many people suggest (and a good suggestion, I agree) to put your code in the jquery load method's callback, on the calling page and not the called page. However I have unique scripts that may appear in certain resources, so I would not want to do that for every load and nor do I necessarily know what these scripts will be.

下面是一个测试设置来证明什么,我要怎样做。 简短的总结是,当我打开 partial.htm main.htm中,它的脚本不火

Here is a test setup to demonstrate what I'm trying to do. The short summary is that when I load partial.htm from main.htm, its script does not fire.

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>

确定,该页面非常简单的功能。想象一下,有更多的链接,其中有些可能需要编写脚本,而有的则没有。

OK, very simple functionality on this page. Imagine there are many more links, and some of them may require scripting while others do not.

下面是 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()方法的回调。 (这将是因为我可能没有脱颖而出,知识,脚本,这些部分可能包含或要求!)

Notice that my script in partial.htm does not fire. So, my question remains: how to get this to fire, excluding any answers that tell me to put this in the .load() method's callback. (This would be because I may not have the fore-knowledge of which scripts these partial pages may contain or require!)

感谢您!


更新#1:

我想一个可以接受的答案是简单的你不能。不过,我想知道这是否是明确的情况下。我一直没能找到任何正式声明这呢。

I suppose an acceptable answer is simply "you can't." However, I'd like to know if this is definitively the case. I haven't been able to find anything that officially states this yet.

此外,当我用萤火虫来检查面板区域之后,没有脚本元素present的。这是因为,如果它被解析由负荷

Also, when I use firebug to inspect the panel region afterwards, there is no script element present at all. It is as if it is being parsed out by load.


更新#2:

我已经缩小下来是一个问题的仅使用选择作为将href的一部分,当的。加载整个somepage.html将执行脚本,但加载somepage.html #someregion

I've narrowed this down to be a problem only when using the selector as part of the href. Loading the entire "somepage.html" will execute the script, but loading "somepage.html #someregion" does not.

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

我要去尝试和追捕,为什么这可能是jQuery的源代码的情况下...

I'm going to try and hunt down why this may be the case in the jquery source...

推荐答案

那么它似乎这是由设计。显然是为了让IE快乐,我们其余的人深受其害。以下是有关code在jQuery代码:

Well it seems that this is by design. Apparently to make IE happy, the rest of us suffer. Here's the relevant code in the jquery source:

// 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开心一些其他的方式?我仍然还没有找到任何东西在网络上讨论这件事情,我敢肯定,我不是这个难倒的唯一的人?

I'm wondering if, instead of just stripping out the scripts, I could modify the jquery source to include them in some other way that makes IE happy? I still have yet to find anything else on the web discussing this matter, I'm sure I'm not the only person stumped by this?

这篇关于如何获得脚本火灾嵌入在通过jQuery的load()方法检索到的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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