Redirect cookie once per session


/ Published in: JavaScript
Save to your folder(s)

Redirect a splash page using javascript. This snippet allows one redirect per session making the backwards navigation possible using the back button.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. // taken from http://evolt.org/article/list/20/416/
  3. // <![CDATA[
  4. var Splash = GetCookie('SplashSkip');
  5. var ReDirected = GetCookie('SplashReDirected');
  6.  
  7. ReDirect('/welcome');
  8.  
  9. function ReDirect (URL) {
  10.  
  11. SetCookie('SplashSkip','TRUE',1);
  12.  
  13. if (Splash == 'TRUE' && ReDirected != 'TRUE' ) {
  14. SetCookie('SplashReDirected','TRUE');
  15. window.location=(URL);
  16. }
  17. }
  18.  
  19. function getCookieVal (offset) {
  20. var endstr = document.cookie.indexOf (";", offset);
  21. if (endstr == -1)
  22. endstr = document.cookie.length;
  23. return unescape(document.cookie.substring(offset, endstr));
  24. }
  25.  
  26. function GetCookie (name) {
  27. var arg = name + "=";
  28. var alen = arg.length;
  29. var clen = document.cookie.length;
  30. var i = 0;
  31. while (i < clen) {
  32. var j = i + alen;
  33. if (document.cookie.substring(i, j) == arg)
  34. return getCookieVal (j);
  35. i = document.cookie.indexOf(" ", i) + 1;
  36. if (i == 0) break;
  37. }
  38. return null;
  39. }
  40.  
  41. function SetCookie(name, value, expDays, path, domain, secure) {
  42. // Set cookie with name, value etc provided
  43. // in function call and date from above
  44. // Number of days the cookie should persist NB expDays='' or undef. => non-persistent
  45. if (expDays != null ) {
  46. var expires = new Date();
  47. expires.setTime(expires.getTime() + (expDays*24*60*60*1000));
  48. }
  49. var curCookie = name + "=" + escape(value) +
  50. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  51. ((path) ? "; path=" + path : "") +
  52. ((domain) ? "; domain=" + domain : "") +
  53. ((secure) ? "; secure" : "");
  54. document.cookie = curCookie;
  55. }
  56. // ]]>
  57. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.