javascript Manage cookies setcookie and getcookie

javascript Manage cookies setcookie and getcookie

Today, We want to share with you javascript Manage cookies setcookie and getcookie.
In this post we will show you setcookie and getcookie using javascript example steps, hear for AngularJS, JavaScript, and jQuery Manage cookies we will give you demo and example for implement.
In this post, we will learn about How to Create, Read, Update and Delete a Cookie with JavaScript with an example.

Today, We want to share with you .
In this post we will show you , hear for we will give you demo and example for implement.
In this post, we will learn about with an example.

Welcome to the In Pakainfo.com website! You will Step By Step learn web programming, easy and very fun. This website allmost provides you with a complete web programming tutorial presented in an easy-to-follow manner. Each web programming tutorial has all the practical examples with web programming script and screenshots available.For setcookie and getcookie using javascript example steps

Persistent simple data is needed cach for things like login status details, session data, tracking user/account or get user information store browser, shopping cart.

index.html

This page tells we how to get/set function to cookies using JavaScript code, how to check set if cookie is enabled or disabled, and how http request cookie works.

<div class="row">
	<div class="col s12 m12 l12 xl10 offset-xl1 center-align">
		<ul class="tabs tabs-fixed-width tab-demo z-depth-1">
			<li class="tab col s3"><a href="#menu1" class="active"  to="people" id="home" onclick="set_activetab('home')">Home</a></li>
			<li class="tab col s3"><a href="#menu2" id="Contact" onclick="set_activetab('Contact')">Contact</a></li>
			<li class="tab col s3"><a href="#menu3" id="About" onclick="set_activetab('About')">About</a></li>
			<li class="tab col s3"><a href="#menu4" id="Project" onclick="set_activetab('Project')">Project</a></li>
		</ul>
	</div>
</div>
<div class="form-inner-data" id="menu1">
	<div class="row">
	123
	</div>
</div>
<div class="form-inner-data" id="menu2">
	<div class="row">
	1234
	</div>
</div>
<div class="form-inner-data" id="menu3">
	<div class="row">
	12345
	</div>
</div>
<div class="form-inner-data" id="menu4">
	<div class="row">
	123456
	</div>
</div>

index.js

HTTP cookie is a one type of the mechanism to all the store data in a web browser Like as a Mozila,chrome for a website, for the purpose of al the persistent data.

<script>
$(document).ready(function() {
set_active_tab();
});	
function set_activetab(vals)
{
	setCookie('selected_tab',vals);
}

function set_active_tab()
{
	var get_cookie = getCookie('selected_tab');
	
	if(get_cookie != '')
	{
		var menu_id = "#"+get_cookie;
		
		$(menu_id).click();

	}
	else
	{
	$('.active').click(function(){
		var active_id = $(this).attr('id');
	});
	// set cookie 
	setCookie('selected_tab',active_id);
	}
}

/*cookie function*/
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
 
function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1);
		if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
	}
	return "";
}
</script>

Storing Cookies Full Example

<html>
   <head>
      <title>Set cookie and get cookie with JavaScript</title>
	   <link rel="shortcut icon" href="https://www.pakainfo.com/wp-content/uploads/2017/01/cropped-pakainfo-1-32x32.png" sizes="32x32" />
      <script type = "text/javascript">
    
            function setCookiefunction()
            {
               if( document.itemform.client.value == "" ){
                  alert("simple Enter to the some value!");
                  return;
               }
               cookievalue= escape(document.itemform.client.value) + ";";
               document.cookie="cookie name=" + cookievalue;
               document.write ("cookie to the Setting Cookies : " + "cookie name=" + cookievalue );
            }
      
      </script>
      
   </head>
   
   <body>
   
      <form name="itemform" action="">
         Enter Login User Name: <input type="text" name="client"/>
         <input type="button" value="Set click to Cookie" onclick="setCookiefunction();"/>
      </form>
   
   </body>
</html>

Reading Cookies Full Example

<html>
   <head>
    <link rel="shortcut icon" href="https://www.pakainfo.com/wp-content/uploads/2017/01/cropped-pakainfo-1-32x32.png" sizes="32x32" />
   <title>Set cookie and get cookie with JavaScript</title>
      <script type="text/javascript">
            function ReadDataCokfunc()
            {
               var allcookies = document.cookie;
               document.write ("cookie - All Cookies : " + allcookies );
               

               cookiearray = allcookies.split(';');

               for(var i=0; i<cookiearray.length; i++){
                  name = cookiearray[i].split('=')[0];
                  value = cookiearray[i].split('=')[1];
                  document.write ("cookie Key is : " + name + " and Value is : " + value);
               }
            }
     
      </script>
      
   </head>
   <body>
      
      <form name="itemform" action="">
         <p> Please click the following press button and see the here result:</p>
         <input type="button" value="Get Cookie" onclick="ReadDataCokfunc()"/>
      </form>
      
   </body>
</html>

Deleting a Cookie Full Example

<html>
   <head>
    <link rel="shortcut icon" href="https://www.pakainfo.com/wp-content/uploads/2017/01/cropped-pakainfo-1-32x32.png" sizes="32x32" />
   <title>Set cookie and get cookie with JavaScript</title>
      <script type="text/javascript">
            function setCookiefunction()
            {
               var now = new Date();
               now.setMonth( now.getMonth() - 1 );
               cookievalue = escape(document.itemform.client.value) + ";"
               
               document.cookie="cookie name=" + cookievalue;
               document.cookie = "cookie expires=" + now.toUTCString() + ";"
               document.write("here all the Setting Cookies : " + "name=" + cookievalue );
            }
      </script>
   </head>
   <body>
   
      <form name="itemform" action="">
         Enter Login User Name: <input type="text" name="client"/>
         <input type="button" value="Click to Set Cookie" onclick="setCookiefunction()"/>
      </form>
      
   </body>
</html>

javascript


Related FAQ

Here are some more FAQ related to this Article:

  1.   DataTables Checkbox Check All Example
  2.   AngularJS Arrays - tips, tricks and examples
  3.   Simple Laravel Log Viewer API Example
  4.   Laravel 6.2 jQuery AJAX CRUD Tutorial
  5.   jquery Compress or resize Image File Size before Uploading PHP Code
  6.   Get Content and Attributes using JavaScript jQuery
  7.   Laravel 6 Eloquent LEFT JOIN Query Example Tutorial
  8.   Auto Post on Facebook Page using Facebook PHP API
  9.   Laravel 7 Implement Flash Messages with example
  10.   jquery Count Number of characters in string input