2014-01-07 118 views
0

我已经打开一个外部URL作为弹出窗口。如何使用javascript在弹出窗口中捕获事件?

var myWindow = window.open("http://google.com/","MsgWindow","width=500,height=600"); 

现在,我需要检查用户是否点击搜索按钮。

我尝试使用jQuery选择像

myWindow.$('#gbqfba').onClick(function(){ 
    alert('abc'); 
}); 

选择但是,这是行不通的。 :(你能帮助我..

在此先感谢。

+0

所以你想一个警报追加到谷歌上的搜索按钮? – Fallenreaper

+0

如果用户点击搜索按钮,我需要触发警报。就这样。 – Sriraman

+0

尝试:$('#gbqfba“)。on(”click“,function(){alert(”abc“);}); – Fallenreaper

回答

1

这只能在两个页面在同一个域下时才有效。

这将您的控制台上工作,而你在stackoverflow.com:

var myWindow = window.open("http://stackoverflow.com/","MsgWindow", "width=500","height=600"); 
$(myWindow).on('click', 'a', function() {alert('a')}) 

这不会:

var myWindow = window.open("http://google.com/","MsgWindow", "width=500","height=600"); 
$(myWindow).on('click', 'a', function() {alert('a')}) 
0

为什么你就不能捕捉搜索按钮的点击?

<button type="button" id="search">Search</button> 

,并在您的jQuery

$("#search").click(function(){ 
    var myWindow = window.open("http://google.com/","MsgWindow","width=500,height=600"); 

}); 
0

我不认为你可以使用javascript来检测事件发生在不同的窗口 你可以尝试,而不是使用window.open(),加载页面(google.com在你的cas e)以lightbox或类似js插件的形式发布到iframe。

即使你这样做了,浏览器也不会让你利用iframe中的内容。您将无法准确检测到用户点击的内容,但只要用户使用此脚本触发了单击事件,您就可以找到它。

$('window').click(function(e){ 
    console.log('User Clicked outside the iframe'); 
}); 

$('window').blur(function(e){ 
    console.log('User clicked on the iframe or outside the page'); 
});​ 
相关问题