Advertisement
amrul813

random.html

Oct 1st, 2023 (edited)
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.62 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Redirecting...</title>
  6.     <script type="text/javascript">
  7.         /*
  8.         This file randomly redirects a user to one of the URLs in a list. It is built
  9.         with the intent of randomization in a controlled experiment.
  10.  
  11.         For example, create three Google Docs forms (or equivalent) and add their URLs
  12.         to the list below. That's it!
  13.  
  14.         The user is then automatically and randomly assigned to one of the URLs when
  15.         the user visits this file. If the user decides to close and reopen the browser,
  16.         the user is still redirected to the same URL since a cookie is saved in the
  17.         user's browser.
  18.        
  19.         Peter M. Dahlgren
  20.         @peterdalle
  21.         peterdahlgren.com
  22.         2016-01-05
  23.         */
  24.        
  25.         // List of URLs (two or more) for the experimental treatment and control groups.
  26.         var RedirectURLs = {
  27.             "urls" : [
  28.                 { "url" : "http://www.google.com/" },
  29.                 { "url" : "http://www.youtube.com/" },
  30.                 { "url" : "https://kusonime.com/" }
  31.             ]
  32.         }
  33.  
  34.         // Count the number of URLs in list.
  35.         var Groups = RedirectURLs.urls.length;
  36.  
  37.         // Randomize number and get the corresponding index for the URL in list.
  38.         var gotoUrl = RedirectURLs.urls[GetRandomNumber(Groups)].url
  39.  
  40.         // Perform redirection.
  41.         if (window.location.search == "?debug") {
  42.             // Debug mode: If user supplies a ?debug querystring,
  43.             // then only show the redirect URL, but don't actually redirect.
  44.             document.write("Redirect URL: " + gotoUrl);
  45.         } else {
  46.             // Redirect the user to the new URL.
  47.             document.location = gotoUrl;
  48.         }
  49.  
  50.         // Perform randomization and save it to a cookie.
  51.         function GetRandomNumber(Groups) {
  52.             // Has cookie been set?
  53.             var rndIndex = null;
  54.             if (GetCookie() != "") {
  55.                 // Yes, get randomization from cookie.
  56.                 rndIndex = GetCookie()
  57.             } else {
  58.                 // No, set random value to cookie.
  59.                 rndIndex = Math.floor((Math.random() * Groups));
  60.                 SetCookie(rndIndex, 30) // Set cookie for 30 days.
  61.             }
  62.             return rndIndex
  63.         }
  64.  
  65.         // Set cookie with randomization value.
  66.         function SetCookie(CookieValue, DaysUntilExpire) {
  67.             var d = new Date();
  68.             d.setTime(d.getTime() + (DaysUntilExpire * 24 * 60 * 60 * 1000));
  69.             document.cookie = "rnd=" + CookieValue + ";expires=" + d.toUTCString();
  70.         }
  71.  
  72.         // Get cookie with randomization value. Return empty string if not set.
  73.         function GetCookie() {
  74.             var ca = document.cookie.split(';');
  75.             for(var i = 0; i < ca.length; i++) {
  76.                 var c = ca[i];
  77.                 while (c.charAt(0) == ' ') c = c.substring(1);
  78.                 if (c.indexOf("rnd=") == 0) return c.substring(4, c.length);
  79.             }
  80.             return "";
  81.         }
  82.     </script>
  83. </head>
  84. <body>
  85. </body>
  86. </html>
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement