簡體   English   中英

使用window.open僅打開一個url實例

[英]Opening only one instance of url using window.open

我正在一個具有超鏈接的asp.net網頁上工作。 只要單擊該超鏈接,就會使用javascript window.open打開一個新的瀏覽器窗口。 我希望如果用戶多次單擊此鏈接,則僅打開一個窗口,而不打開多個窗口。 我只希望當用戶多次單擊該超鏈接時突出​​顯示該窗口。 我是否需要使用window.open來檢測是否在瀏覽器的任何其他選項卡中打開了URL? 是否有為此內置的任何jQuery插件,以便我可以將其用於瀏覽器兼容性。

這是超鏈接URL:

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

請提出建議。

嘗試

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

它應始終在同一窗口中打開。 參見參考

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

要僅打開HTML頁面中彈出窗口的一個實例,請使用window.open methodwindowName參數。
例如

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

每次用戶單擊包含window.open代碼的鏈接時,都會打開一個新窗口。
相反,

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

無論用戶單擊鏈接多少次,都只會打開一個窗口實例。

您還可以按以下方式使用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>

編輯1

<a onclick="return 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");
    return false;
}
<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM