最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Opening only one instance of url using window.open - Stack Overflow

programmeradmin3浏览0评论

I am working on an asp 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.open to 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 patibility.

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.

I am working on an asp 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.open to 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 patibility.

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.

Share Improve this question edited May 20, 2015 at 17:43 Jonathan Hall 79.9k19 gold badges159 silver badges203 bronze badges asked Jun 7, 2013 at 6:24 DotnetSparrowDotnetSparrow 28k64 gold badges187 silver badges318 bronze badges 4
  • Can't you just have a javascript call on button click and set a variable the first time it is clicked? Then if the variable is set, don't open a new window.. – Bulbasaur Commented Jun 7, 2013 at 6:32
  • Hi I think you can use window.showModalDialog instead of window.open – Neeraj Commented Jun 7, 2013 at 6:33
  • What code are you using to window.open – Amit Garg Commented Jun 7, 2013 at 6:44
  • I am using window.open("LoginAPI?ID=" + encodeURIComponent(ID) + "&PSSWD=" + encodeURIComponent(PSSWD) + "&ACCID=" + encodeURIComponent(ACCID) + "&PASSWDINT=" + encodeURIComponent(PASSWDINT) + "", "LoginAPI"); – DotnetSparrow Commented Jun 7, 2013 at 6:45
Add a ment  | 

4 Answers 4

Reset to default 3

Try

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

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

HTML:

<a href="http://www.someurl." 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);
}
<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./",
   "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/en-US/docs/Web/API/window.open

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

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

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

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

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

you can also use focus function as used below

<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

<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;
}
发布评论

评论列表(0)

  1. 暂无评论