Check if a window is open and opens a new window only once?

I use the code below to open a new window when a user crashes from my site using the jquery ready document function.

<script type="text/javascript"> $(document).ready(function(){ window.open('link','title','width=460,height=500,status=no,scrollbars=yes,toolbar=0,menubar=no,resizable=yes,top=460,left=600'); return false }); </script> 

However, he continues to go to every page with this code. What I want to do is simply exit this ONCE window for users who do not have this window. If the user has opened this window, and he will no longer pull out a new one.

So how can I do this?

+4
source share
6 answers

give the window a name

If a window with the name strWindowName already exists, then strUrl is loaded into the existing window.

just make sure your window openers use the same name so that they open in the same window if a window with that name is already open. (wooh! tongie twister!)

+5
source

You can try to add a cookie to users who open the window. Check it out on every page. And don't forget to remove it when the window is closed.
About JS Cookies

+1
source

Use browser cookies . This is a way to store some โ€œvariablesโ€ on multiple pages that you can only manipulate in your domain.

Since you are using jQuery, I suggest you use this plugin .

So the code is as simple as:

 $(document).ready(function() { if ($.cookie('opened') !== null) { window.open([...]) $.cookie('opened', 'is') } }) 
0
source
  <script type="text/javascript"> $(function () { //get the complete queryString (url) for the popup page var url = document.URL; //use sessionStorage to control whether the page has been opened or not //try get the sessionStorge name, if = nothing, open page if (sessionStorage.getItem('visited') == null) { //then set a sessionStorage name and value, //it is important that this line appears BEFORE the window.open //else you will get a loop because ==null then is always true //at last set a sessionStorage value so it no longer ==null //and open window - once. sessionStorage.setItem("visited", "Ja"); window.open(url, '_blank', 'width=750, height=1010'); } }); </script> 

This popup will open only once, no cookies stored in the client browser, and the next time you open the browser, sessionStorage will disappear. (Sample $ .cookie ('open') does not work!) Regards, Ola Balstad

0
source

Add variable to file as flag

 <script type="text/javascript"> var myWindowFlag = false; $(document).ready(function(){ if(!myWindowFlag){ window.open('link','title','width=460,height=500,status=no,scrollbars=yes,toolbar=0,menubar=no,resizable=yes,top=460,left=600'); myWindowFlag= true;} return false }); </script> 

it can help you

EDIT: we can do as @devjosh said

 <script type="text/javascript"> $(document).ready(function(){ if(!getCookie(myWindowFlag)){ window.open('link','title','width=460,height=500,status=no,scrollbars=yes,toolbar=0,menubar=no,resizable=yes,top=460,left=600'); } return false }); </script> 

and in your new window reset cookie onload value

-3
source

Source: https://habr.com/ru/post/1402604/


All Articles