0
votes

I have a hyper-link in my php page which opens in new window when it is clicked. But I want it should open only in one window at time, it should ne open if it is already open in new window of browser. Suppose If I click 5 times it is opening in 5 new windows, I want it only in one window. This problem is coming with Mozilla Firefox Please help me doing this with JavaScript or Jquery. Thanks thanks. and sorry to share incomplete information. Actually multiple links will be created through php code, which will be opened in different windows. I have used JavaScript to open them in different window. My problem is that same url should not open again if it is already open in window or if any url is clicked multiple times it should open only one window. Here is my code : PHP Code

<?php

$Class="class='links'"; $jsfun = 'javascript:openMyLink';

while(condition) { print "Click Here";

} ?>

JS code :

function openMyLink(arg1,arg2,arg3) { var url = "http:///launcher.php?arg1="+arg1+"&arg2="+arg2+"&arg3="+arg3; newwindow = window.open(url, "Mywindow",'height=596, width=795, left=0, top=0, resizable=yes, scrollbars=no, toolbar=no, menubar=no'); }

I tried to change the second parameter of window.open as dynamic also instead of "Mywindow" but it did not work too. Its not giving the issue with chrome or IE.

2
Are we supposed to guess what your flawed code is and then suggest a fix? Opening/preventing a second window is a javascript/html operation, you can make a working sample in few lines. You should edit your question to include the code, or better(?) yet, put a working copy onto jsfiddle.netenhzflep
If you are using window.open, set the second parameter as a specific value, like "a1", and it should only allow one "instance" of the window open at a time.Ian

2 Answers

3
votes

If you want to open a site in new window only once you clicked on it, use below code...

<a id="target" href="javascript:void(0)" onClick="myfunc()">click here</a>
<script type="text/javascript">
x=0;
function myfunc()
{   
    if(x==0)
    {
        x+=1;
        window.open("<--- YOUR SITE URL --->");
    }
}
</script>
0
votes

On your first page have this code,

jQuery(document).ready(function($){

     $("#theLink").click(function(e){
         if($(this).attr("at")=='1')
         { 
              e.preventDefault();
              return false;
         }
  });
});

and your link should be having an attribute 'at' set to '0' just like

<a id="theLink" href="demo.html" target="_blank" at="0">Click here to go away</a>

and let your children or newly opened page have this code.

window.opener.$("#theLink").attr("at","1");
$(window).unload(function() {    
   window.opener.$("#theLink").attr("at","0");
});

This is little tricky but works.

Worked great for me on IE8.