capri
capri

Reputation: 2127

How to set popup on page load first time only?

How to set a popup window that open when first time the page load? i m using this code for my popup how to set session for this popup? is there any way to use ip as session?

    <script>
        !window.jQuery && document.write('<script src="fancybox/jquery-1.4.3.min.js"><\/script>');
    </script>

    <script type="text/javascript" src="fancybox/jquery.fancybox-1.3.4.pack.js"></script>   

    <script type="text/javascript">

    $(document).ready(function() {


        $("a#example1").fancybox();

        $("a#example1").trigger('click');


    });
    </script>


    <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.4.css" media="screen" />


</head>



<body>

<a id="example1" href="images/pic.jpg"></a> 



</body>

Upvotes: 0

Views: 11113

Answers (4)

Pushpendu Mondal
Pushpendu Mondal

Reputation: 151

You can you Cookies or localStorage Using Cookies:

$(document).ready(function() {
var loadfirst = getCookie("loadfirsttime");
if(loadfirst == null){
setCookie("loadfirst", "again" 1); // 1 is the number of days
$("a#example1").fancybox();
 $("a#example1").trigger('click');
}
});

Using LocalStorage:

$(document).ready(function() {
 var loadfirst = localStorage.getItem("loadfirsttime");
if(loadfirst == null){
localStorage.setItem('loadfirst ', 1); // 1 is value assigned.
$("a#example1").fancybox();
 $("a#example1").trigger('click');
}
});

Thanks

Upvotes: 0

mgraph
mgraph

Reputation: 15338

you can use jquery-cookie

Demo :

$(document).ready(function() {
       if($.cookie('popup') != 1){
           $.cookie('popup', '1');
           $("a#example1").fancybox();
           $("a#example1").trigger('click');
        }
    });

Upvotes: 2

Ananth
Ananth

Reputation: 4397

Using sessions for this would be an unnecessary load on your server. Use cookies instead, that helps to store data in the user's computer.

Use Javascript/your server language to check the cookie and show the popup based on its value!

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074266

Check for a cookie, and if not there, do the popup and set the cookie for next time; if the cookie is there, don't do the popup. Quirksmode has some functions for making cookies easier, or of course there's the jQuery cookie plugin (and probably about 50 others).

Upvotes: 3

Related Questions