mercredi 15 novembre 2017

Redirect in javascript: IF GET parameter key allow to stay on the page; otherwise redirect to another page

How is the best way to write this code? We want to let people who know how to add the ?Redirect=No to append the url stay on the page and view it otherwise the person will get redirected to the new site or to a "we-have-moved" page. An example would help me very much in knowing how to detect the GET url use case. I am a noobie javascript coder so your help is much appreciated!

Option #1

  • USE CASE 1: oldsite.mysite.com : redirects to new site Use case
  • USE CASE 2: oldsite.mysite.com?Redirect=No : does not redirect and the page loads normally

Option #2 (A more user friendly approach)

  • Use case 1: oldsite.mysite.com : site redirects immediately to new site

  • Use case 2: oldsite.mysite.com?Redirect=No : does not redirect and the page loads normally

  • Use case 3: http://ift.tt/2huGMSb : redirects to a “we’ve moved page” and then after “x” seconds redirects to the new site.

  • Use case 4: http://ift.tt/2zJqOxW : does not redirect and the page loads normally


<script>

// Check if the GET url parameter of "redirect=NO" is appended to the url 
and redirect if there is no parameter defined.


$(function() {


  if (  ... //url equals oldsite.mysite.com ...  )

      //GET redirect=NO so do nothing and stay here

      window.location.href = "http://ift.tt/2hvIKC8";

     }



   else if ( ... //url equals http://ift.tt/2zHGXUO ...  )

     //GET redirect=NO so do nothing and stay here

     window.location.href = "http://ift.tt/2hwxfdy";

  }

 });

</script>

OR would it look more like this?

<script>

switch(expression) {

    case n:
         //GET url contains parameter "Redirect=NO"
         break;

    case n:
         if (//url contains a child page name) {

          window.location.href = "http://ift.tt/2zJ1DvJ";

             }

         break;

   default:
         window.location.href = "http://ift.tt/2hvIKC8"; 

 }

</script>

Aucun commentaire:

Enregistrer un commentaire