Advertisement
urosevic

Read/Write Cookies and Read URL Parameter

Jan 20th, 2016
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* AU:20140603 Cookie drop and read + get URL parameter value */
  2. function getCookie(cname) {
  3.     var name = cname + "=";
  4.     var ca = document.cookie.split(';');
  5.     for(var i=0; i<ca.length; i++) {
  6.         var c = ca[i].trim();
  7.         if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
  8.     }
  9.     return "";
  10. }
  11. function setCookie(cname, cvalue, exdays, cpath) {
  12.     var d = new Date();
  13.     d.setTime(d.getTime() + (exdays*24*60*60*1000));
  14.     var expires = "expires=" + d.toGMTString();
  15.     document.cookie = cname + "=" + cvalue + "; " + expires + "; path=" + cpath;
  16. }
  17. function getURLParameter(name) {
  18.   return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
  19. }
Advertisement
RAW Paste Data Copied
Advertisement