Create cookie using javascript

in bellow JavaScript function are create cookie and get cookie with path, expire time.

// Create new Cookies
	setCookie("dalwadi_tab", 'dalwaditab', 30,'admin');
	
	// get Cookies
	var activeTab=getCookie("dalwadi_tab");	
	alert(activeTab);
	
	
	// Create new Cookies function
	function setCookie(Cookiename,Cookievalue,exdays,Cookiepath) {
		var d = new Date();
		d.setTime(d.getTime() + (exdays*24*60*60*1000));
		var expires = "expires=" + d.toGMTString();
		document.cookie = Cookiename+"="+Cookievalue+"; "+expires + path+"/"+Cookiepath;
	}
	// get Cookies function
	function getCookie(Cookiename) {	
		var name = Cookiename + "=";
		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 "";
	}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s