简体   繁体   English

将数据从弹出窗口传递回javascript

[英]Pass data from pop-up window back to javascript

I have a window.open function in javascript that calls a page. 我在调用页面的javascript中有一个window.open函数。 I then want to grab the data from that page and return it to the javascript function. 然后,我想从该页面获取数据并将其返回给javascript函数。

Can anyone help me do this? 有人可以帮我吗?

$("#popup").click(function(e) { 
    var url = $(this).attr('href');
    e.preventDefault();
    window.open(url, 'authWindow', "width=800,height=436");
});

If the page in the popup is in the same domain as yours, you can use window.opener to access data of the opener windows 如果弹出窗口中的页面与您所在的位于同一域中,则可以使用window.opener来访问打开器窗口的数据

In your current window : 在当前窗口中:

var winopened;
$("#popup").click(function(e) { 
    var url = $(this).attr('href');
    e.preventDefault();
    winopened = window.open(url, 'authWindow', "width=800,height=436");
});

if(winopened) {
  // call a function in the opened window : 
  res = winopened.functionInWindow();

}

hope it helped ... 希望它能帮助...

window.open will return your window object. window.open将返回您的window对象。

var myWindow = window.open

Now you can play with myWindow. 现在您可以使用myWindow了。

The other posters answers are correct, in that they'll give you the window object. 其他张贴者答案是正确的,因为它们将为您提供窗口对象。 I'm not sure that's what you were asking for. 我不确定这就是您要的。

Here's a very simple way you can return the HTML from the page, if that's the "data" you need (otherwise disregard this answer). 如果您需要的是“数据”,这是一种非常简单的方法,您可以从页面返回HTML(否则忽略此答案)。

This uses jQuery's AJAX $.get function to load HTML from a webpage, more at https://api.jquery.com/jQuery.get/ 它使用jQuery的AJAX $ .get函数从网页加载HTML,更多信息请参见https://api.jquery.com/jQuery.get/

$.get(url, function( data ) {
   alert( "Data Loaded: " + data );
});

From here, you can do whatever you like with the "data" variable that is returned - store it, parse it, show it... 在这里,您可以对返回的“数据”变量进行任何操作-存储,解析,显示...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM