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??
16 Answers
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!)
2You 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
This may help.
1Use 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