Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set session variable in jquery?

I have an html page that open a popup window when the page loads.

I need to set the popup only when the page open first time. I think session or cookie is to be set.

    <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> 
like image 289
capri Avatar asked Jun 20 '12 16:06

capri


People also ask

Can we set session value in jQuery?

jQuery is a JavaScript library and JavaScript is a Client Side language and hence directly it is not possible to set Session variable in jQuery.

How to set session value in jQuery AJAX?

To set the Session value you need to make ajax call to ActionResult and pass the id and text as parameter and set the session inside the ActionResult and redirect to the specified page to do the rest of functionality. // Set Session values. Then in the report controller get the Session Values and do rest of task.

How can use session data in jQuery?

To use session data ,you don't need jquery you can use setItem ,getItem and removeItem to deal with the session data which is stored as a json object and accessible via key.

How to set session value in jQuery in ASP net?

jQuery is a Javascript library, which is a client-side language. Setting a session variable using a client-side function is not possible. So we make an Ajax Call using a jQuery to pass the variable value to the controller and set a session variable. Now let get started by creating a model Country as shown below.


1 Answers

Use localStorage to store the fact that you opened the page :

$(document).ready(function() {     var yetVisited = localStorage['visited'];     if (!yetVisited) {         // open popup         localStorage['visited'] = "yes";     } }); 
like image 141
Denys Séguret Avatar answered Sep 18 '22 01:09

Denys Séguret