javascript 使用 window.open 只打开一个 url 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16977590/
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-27 06:39:07  来源:igfitidea点击:

Opening only one instance of url using window.open

javascriptjqueryasp.net

提问by DotnetSparrow

I am working on an asp.net web page which has a hyperlink. when ever that hyperlink is clicked, a new browser window is opened using javascript window.open. I want that If user clicks this link multiple times, then only one window is opened and not multiple windows. I just want that window to be highlighted when user clicks that hyperlink multiple times. Do I need to use window.opento detect if the url is opened in any other tab of the browser ? Is there any jQuery plugin built in for this so that I can use it for browser compatibility.

我正在处理一个具有超链接的 asp.net 网页。当点击该超链接时,会使用 javascript 打开一个新的浏览器窗口window.open。我希望如果用户多次单击此链接,则只打开一个窗口而不是多个窗口。我只想在用户多次单击该超链接时突出​​显示该窗口。我是否需要使用window.open来检测 url 是否在浏览器的任何其他选项卡中打开?是否有为此内置的任何 jQuery 插件,以便我可以使用它来实现浏览器兼容性。

Here is the hyperlink url:

这是超链接网址:

<a onclick="addClick()" href="javascript:void(0)">
                    New</a>

and here is the code I am using:

这是我正在使用的代码:

function addClick() {
    var ID = jQuery("#ID").val();
    var PSSWD = jQuery("#PSSWD").val();
    var ACCID = jQuery("#ACCID").val();
    var PASSWDINT = jQuery("#PASSWDINT").val();

    window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
}

Please suggest.

请建议。

回答by dan radu

Try

尝试

window.open("<url>", "<window name>");

This should always open in the same window. See reference.

这应该始终在同一窗口中打开。见参考

回答by karaxuna

HTML:

HTML:

<a href="http://www.someurl.com" onclick="openwindow.call(this); return false;">open window</a>

var wins = {};
function openwindow(){
    var url = this.href;
    if(typeof wins[url] === 'undefined' || wins[url].closed)
        wins[url] = window.open(url);
}

回答by ????

To open only one instance of a popup window in an HTML page, use the windowNameparameter of the window.open method.
For example

要在HTML页面中只能打开一个弹出式窗口的情况下,使用windowName的参数window.open method
例如

window.open('http://www.abc.com') 

will open a new window each time the user clicks the link containing the window.open code.
In constrast,

每次用户单击包含 window.open 代码的链接时,都会打开一个新窗口。
相比之下,

window.open('http://www.abc.com','abc') 

will open only one instance of the window, no matter how many times users click the link.

无论用户单击链接多少次,都将只打开窗口的一个实例。

you can also use focusfunction as used below

您还可以使用focus下面使用的功能

<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
    newwindow=window.open(url,'name','height=200,width=150');
    if (window.focus) {newwindow.focus()}

      if (!newwindow.closed) {newwindow.focus()}
    return false;
}

// -->
</script>

Edit 1

编辑 1

<a onclick="return addClick()" href="javascript:void(0)">New</a>

and here is the code I am using:

这是我正在使用的代码:

function addClick() {
    var ID = jQuery("#ID").val();
    var PSSWD = jQuery("#PSSWD").val();
    var ACCID = jQuery("#ACCID").val();
    var PASSWDINT = jQuery("#PASSWDINT").val();

    window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI");
    return false;
}

回答by Amit Garg

<script>


var windowObjectReference = null; // global variable

function openFFPromotionPopup() {
  if(windowObjectReference == null || windowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    windowObjectReference = window.open("http://www.spreadfirefox.com/",
   "PromoteFirefoxWindowName", "resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    windowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
</script>
<a href="javascript:void(0);" onclick="openFFPromotionPopup()">click here</a>

Check the reference https://developer.mozilla.org/en-US/docs/Web/API/window.open

检查参考https://developer.mozilla.org/en-US/docs/Web/API/window.open