window.open和$(document).ready [英] window.open and $(document).ready
问题描述
我试图制作一个可打开弹出窗口的小书签.在此窗口内的是CSS类列表,这些类一旦被选中,就会在window.opener
页面上突出显示该对象.所以我遇到了两个问题.
I am trying to make a bookmarklet that opens a popup window. Inside this window is a list of CSS classes that once selected, highlights that object on window.opener
page. So I'm running into two problems.
- Firebug在弹出窗口中不起作用,所以我看不到发生了什么事.
- 窗口永远不会完成加载(至少我可以在Firefox中知道),因此窗口内的
$(document).ready(function(){...})
永远不会执行.
- Firebug doesn't work in the popup window, so I can't see what's going on.
- The window never finishes loading (at least I can tell in Firefox) so the
$(document).ready(function(){...})
inside the window never gets executed.
由于遇到跨域问题,所以无法从远程位置打开弹出窗口.这是一些示例代码:
I can't open the popup from a remote location because I run into cross domain issues. Here is some sample code:
<script type="text/javascript">
function makepopup(){
var popup = '<!DOC'+'TYPE HT'+'ML PUBLIC "-//W3C//DTD HT'+'ML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' +
'<ht'+'ml><he'+'ad><title>Test</title>' +
'<scr'+'ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></scr'+'ipt>' +
'</he'+'ad><bo'+'dy>' +
'<div id="wrap">' +
'testing popup' +
'</div>' +
'<input type="button" value="Click Me" />' +
'<scr'+'ipt type="text/javascript">' +
'$(document).ready(function(){' +
'$(":input").click(function(){ alert($(window.opener.doc'+'ument).find("#test").html()) });' +
'})' +
'</scr'+'ipt>' +
'</bo'+'dy></ht'+'ml>';
var testpopup = window.open( '','test','toolbar=1,location=0,status=0,width=500,height=450,scrollbars=1' );
testpopup.document.write(popup);
return false;
}
</script>
<a href="#" onclick="javascript:makepopup()">Open popup</a>
<div id="test" style="display:none">This is hidden text</div>
如果我在弹出窗口$(":input").click(function(){ alert($(window.opener.document).find("#test").html()) });
中将以下内容添加到控制台中,则可以正常工作,因此我确定它永远不会被调用.
If I add the following to the console in the popup $(":input").click(function(){ alert($(window.opener.document).find("#test").html()) });
, it works fine, so I'm sure it the document.ready never being called
或者,有更好的方法吗?
Or, is there a better way to do this?
推荐答案
如果在return false
之前添加以下行,则$(document).ready
是否会触发?
Does the $(document).ready
fire if you add the following line before the return false
?
testpopup.document.close();
尽管这是一个疯狂的猜测,但我尚未对此进行测试.
This is a wild guess though, and I haven't tested this.
这篇关于window.open和$(document).ready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!