javascript 如何在不刷新的情况下将焦点设置到子窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6032101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:12:43  来源:igfitidea点击:

How to set the focus to a child window without refreshing it?

javascript

提问by Ekta

I am having a web page which contain four links, each link open a popup window. I am using a common JavaScript function to open popup window, as given below

我有一个包含四个链接的网页,每个链接打开一个弹出窗口。我正在使用一个常见的 JavaScript 函数来打开弹出窗口,如下所示

function OpenPopup(pageURL, title,w,h) {

    var left = (screen.width/2)-(w/2);

    var top = (screen.height/2)-(h/2);

    var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, 
        directories=no, status=no, menubar=no, scrollbars=Yes, resizable=no, 
        copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);

    targetWin.focus();

}

When I click on a link it will open a popup window, which contain a data entry form, I entered some information into that form. Then I need to review other information, from other popup window, so i go back to my main window, click on another link, a new popup window will get open which contain the information I need to review. I get the information and close this popup window, so now i am on my main window, then again I click on the link for that data entry form, so the popup window comes up but i lost the information that I have entered, it's because I am opening the popup window once again, and the new popup window will replace the existing popup with the same name.

当我点击一个链接时,它会打开一个弹出窗口,其中包含一个数据输入表单,我在该表单中输入了一些信息。然后我需要查看其他信息,从其他弹出窗口,所以我回到我的主窗口,点击另一个链接,一个新的弹出窗口将打开,其中包含我需要查看的信息。我得到了信息并关闭了这个弹出窗口,所以现在我在我的主窗口上,然后我再次点击那个数据输入表单的链接,所以弹出窗口出现了,但我丢失了我输入的信息,这是因为我再次打开弹出窗口,新的弹出窗口将替换现有的同名弹出窗口。

Now my questions are:

现在我的问题是:

  1. Is their any way, I can check out the if popup window is already open from parent window?

  2. If the popup window is already open how can I just set the focus to it, rather then reloading it?

  3. How can I refer a child window from parent window, without having object of child window just by the name of child window?

  4. Can I refer a child window from a parent window, even after refreshing the parent window?

  1. 他们有什么办法吗,我可以检查弹出窗口是否已经从父窗口打开?

  2. 如果弹出窗口已经打开,我怎样才能将焦点设置在它上面,而不是重新加载它?

  3. 如何从父窗口引用子窗口,而没有子窗口的对象仅通过子窗口的名称?

  4. 即使在刷新父窗口之后,我也可以从父窗口引用子窗口吗?

回答by Aravindan R

For 1 and 2 , you can use this function

对于 1 和 2 ,您可以使用此功能

// loads an URL into the popup without reloading it
function popupnr(mylink, windowname, refocus) {
    // open the window with blank url
    var mywin = window.open('', windowname, 'window attrs here');

    // if we just opened the window
    if (mywin.closed || (!mywin.document.URL) || (mywin.document.URL.indexOf("about") == 0)) {
        mywin.location = mylink;
    }
    else if (refocus) {
        mywin.focus();
    }

    // return the window
    return mywin;
}
  1. You cant refer a child window by its name. There is no window.children. You need to call window.opento get a reference.

  2. Yes you can. But u need to use the same window name while calling window.open

  1. 您不能通过名称来引用子窗口。没有窗口。孩子。您需要致电window.open以获取参考。

  2. 是的你可以。但是你需要在调用时使用相同的窗口名称window.open

回答by ds_student

I think what you want for 1 and 2 is:

我认为你想要的 1 和 2 是:

if(!targetWin)
{
    var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, 
        directories=no, status=no, menubar=no, scrollbars=Yes, resizable=no, 
        copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}
targetWin.focus();