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

时间:2013-06-07 06:24:08

标签: javascript jquery asp.net

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

这是超链接网址:

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

这是我正在使用的代码:

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");
}

请建议。

4 个答案:

答案 0 :(得分:3)

尝试

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

这应始终在同一窗口中打开。请参阅reference

答案 1 :(得分:1)

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);
}

答案 2 :(得分:0)

要在HTML页面中仅打开一个弹出窗口的实例,请使用windowName的{​​{1}}参数。
例如

window.open method
每次用户单击包含window.open代码的链接时,

将打开一个新窗口。
相反,

window.open('http://www.abc.com') 
无论用户点击链接多少次,

都只会打开一个窗口实例。

您还可以使用下面的window.open('http://www.abc.com','abc') 功能

focus

编辑1

<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>

这是我正在使用的代码:

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

答案 3 :(得分:0)

<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>

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