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??
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??
Share Improve this question asked Mar 21, 2012 at 7:16 sm21guysm21guy 6263 gold badges13 silver badges33 bronze badges 1- you have to make use of cookies for that see if this link helps web-source/javascript_popup_window3.htm – Devjosh Commented Mar 21, 2012 at 7:20
6 Answers
Reset to default 5give 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!)
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
This may help.
http://www.javascriptkit./javatutors/openclose.shtml
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 plete 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