check if window is open and opens a new window only for once?

i use the code below to open a new window when a user drops by my site using jquery document ready 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 , it keeps on popping out on every single page that have this code. What i want to do is only pop out this window ONCE for users that do not have this window opened. If a user have this window opened and it will no longer pop out a new one.

So how can i do this??

1

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 the window with that name is already open. (wooh! tongue twister!)

2

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

0

This may help.

1

Use browsers' cookies. It is a way to keep some "variables" across multiple pages that you can manipulate on your domain only.

Since you're using jQuery, I'd suggest you use this plugin.

This way, the code is as simple as:

$(document).ready(function() { if ($.cookie('opened') !== null) { window.open([...]) $.cookie('opened', 'is') }
})
 <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 window will open only once, no cookies stored in the client browser and the next time you open the browser, the sessionStorage is gone. (The $.cookie('opened') sample above does not work!) Kind regards, Ola Balstad

Add a variable to the file like a 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>

this might 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 the cookie value onload

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like