2014-06-06 48 views
-1

我编写了一个脚本,用于打开弹出窗口,搜索用户,发现 用户搜索窗口关闭,然后脚本在父窗口中显示新用户。在窗口之间传递并写入

基本上我单独尝试了所有的脚本,但当我在窗口之间传递时遇到问题。前面的代码不会传递找到的用户,但只传递一个示例用户来调试代码。

这里是脚本文件:

var orgWindow   
var searchWindow 

$(document).ready(function(){ 
    $(".add_participants").on("click", function() { 
    var orgWindow = window.self   
var searchWindow = window.open("http://localhost:3000/users/search", "_blank", 
     "left=200, top=100, height=300, width=300, menubar=yes"); 
    }); 
});   

function printUser(){ 
    var a = orgWindow.document.createElement("a"); 
    a.setAttribute("href","https://stackoverflow.com/users/show"); 
    a.setAttribute("id","3"); 

    var aText = orgWindow.document.createTextNode("elad bezalel"); 
    var img = orgWindow.document.createElement("img"); 
    img.setAttribute("alt","El4"); 
    img.setAttribute("src","/uploads/user/image/3/el4.jpg"); 
    img.setAttribute("height","150"); 
    img.setAttribute("width","150"); 

    myDiv = orgWindow.document.getElementById("par"); 

    a.appendChild(img); 

    var brk = orgWindow.document.createElement("br"); 
    a.appendChild(brk); 
    a.appendChild(aText); 

    myDiv.appendChild(a); 
} 

function add_user(){ 
    user_id = $("#button").data("user_id"); 
    window.close(searchWindow); 
    window.blur(searchWindow); 
    window.focus(orgWindow); 
    orgWindow.document.write(printUser()); 
    orgWindow.close(); 
}  

我使用jQuery(我将在稍后解释)。当我点击一个按钮时,搜索窗口打开。然后,我在搜索窗口中找到一个用户,然后单击另一个按钮(添加用户),然后搜索窗口关闭,父窗口变得焦点。现在我试图实现用户图像和名称,但不知何故它不起作用。

我该如何修复脚本?

回答

0

注意,您必须声明orgWindowsearchWindow在两个不同范围的变量,在全球范围内(在顶部的两个变种用法)和当地的点击处理程序。这可能成为你的问题的一部分。

0

我认为两个独立的窗口上下文混杂在一起。

在主add_user页面功能:

function add_user(user_id){ 
    window.close(searchWindow); 
    window.blur(searchWindow); 
    window.focus(orgWindow); 
    orgWindow.document.write(printUser()); 
    orgWindow.close(); 
} 

并在弹出:

$("#button").click($(window.opener).add_user($(this).data("user_id"))); 
相关问题