Snap Builder Public Codes Library
Snap | Generators | Login | Browse | Search | Enter Code | Export Snap Builder Public Codes Library

 

Category: Javascript Codes Library >> Cookies >> Cookie Redirect


Code Snippet </> Info


Snippet Name: Cookie Redirect

Description: When a page has performed it's purpose, a cookie can be set to redirect the browser to a different page on subsequent visits.

Compatible Browsers:
Chrome BrowserFirefox BrowserInternet Explorer BrowserOpera BrowserSafari Browser


Example: View Code Demo


Note:

Author: William Bontrager

Last Modified: 2015-08-26 22:59:14

Language: javascript

Highlight Mode: javascript

Copy Codes: Use Free Notepad ++
Bookmark and Share

Snap HTML Code Editor:
Paste the source code, make changes and instantly see it in live preview.
Snap HTML Code Editor


  About Copying
Copied To Clipboard!

<html> <head> <title>Cookie Redirect Javascript Code Snippet</title> <script> /* Begin Cookie Redirect == This Script Free To Use Providing This Notice Remains == == This Script Has Been Found In The http://snapbuilder.com Free Public Codes Library == == NOTICE: Though This Material May Have Been In A Public Depository, Certain Author Copyright Restrictions May Apply == == Created By: William Bontrager : http://www.bontragerconnection.com/ : Creative Commons License == // // Three items can be customized (values between quotation marks): 1. What URL shall the browser be redirected to if a cookie was previously set? */ var RedirectURL = "http://snapbuilder.com/code_snippets/"; // 2. How many days shall the cookie live in the visitor's browser (0 means remove cookie whenever browser closes)? var DaysToLive = "365"; /* 3. What name shall the cookie have (any sequence of alphabetical characters is okay, so long as the name doesn't conflict with any other cookies that this web page might be setting.)? */ var CookieName = "AlreadyVisited"; // No other customization is required. function Action() { location.href = RedirectURL; } DaysToLive = parseInt(DaysToLive); var Value = 'bypass page next time'; function GetCookie() { var cookiecontent = ''; if(document.cookie.length > 0) { var cookiename = CookieName + '='; var cookiebegin = document.cookie.indexOf(cookiename); var cookieend = 0; if(cookiebegin > -1) { cookiebegin += cookiename.length; cookieend = document.cookie.indexOf(";",cookiebegin); if(cookieend < cookiebegin) { cookieend = document.cookie.length; } cookiecontent = document.cookie.substring(cookiebegin,cookieend); } } if(cookiecontent.length > 0) { return true; } return false; } function SetCookie() { var exp = ''; if(DaysToLive > 0) { var now = new Date(); then = now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000); now.setTime(then); exp = '; expires=' + now.toGMTString(); } document.cookie = CookieName + '=' + Value + exp; return true; } if(GetCookie() == true) { Action(); } SetCookie(); </script> </head> <body> <p>When a page has performed it's purpose, a cookie can be set to redirect the browser to a different page on subsequent visits. Easily implemented.</p> <p>Simply put this code snippet in your document HEAD area.</p> <p>If the visitor has visited your page, they will be redirected to the URL designated.</p> <br /><br /> <div id="snippet"> [ This code example from <a href="http://snapbuilder.com/code_snippets/copy_code_snippet_62.html">Cookie Redirect</a> page. ] </div> </body> </html>


[ Snippet Options ]

 

© 2002 -  Snap Builder Public Codes Library