locked
Redirecting pages based off URL parameter? RRS feed

  • Question

  • User-851246726 posted

    I have a webpage and whoever gets on that page, I want to redirect them to different location.

    So,

    IF current link in address bar is www.site.com/mypage?a=paramApple
    then redirect to www.google.com
    IF current link in address bar is www.site.com/mypage?a=paramMango then redirect to www.yahoo.com else redirect to www.bing.com

    How to do this?

    Wednesday, January 18, 2017 5:14 PM

All replies

  • User527778624 posted

    Hi,

    Try adding this JS code in your webpage:

    window.onload=function(){
     var keys = (window.location.search || "").split("=");
     var url = "//www.bing.com";
     if(keys.length>1){
        if(keys[1].indexOf("paramApple")>-1)
    		url="//www.google.com";
    	else if(keys[1].indexOf("paramMango")>-1)
    		url="//www.yahoo.com";
    	}
    	window.location=url;
    };

    //code not tested.

    Wednesday, January 18, 2017 5:38 PM
  • User-851246726 posted

    Thank you @raju

    So, I am trying to add a counter / timer also so that user will be navigated in 5 seconds and countdown will be displayed on the page so that user know about the redirection.

    This is my code, how can I add countdown to this redirection code?

    <script>
        function startTimer(duration, display) {
            var timer = duration, minutes, seconds;
            var end =setInterval(function () {
                minutes = parseInt(timer / 60, 10)
                seconds = parseInt(timer % 60, 10);
    
                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;
    
                display.textContent = minutes + ":" + seconds;
    
                if (--timer < 0) {
                    window.location = "http://www.mywebsite.com/";
                    clearInterval(end);
                }
            }, 1000);
        }
    
        window.onload = function () {
            var fiveMinutes = 5,
                display = document.querySelector('#time');
            startTimer(fiveMinutes, display);
        };
    </script>
    
    
    
    <script language="javascript">
    window.onload=function(){
     var keys = (window.location.search || "").split("=");
     var url = "//www.bing.com";
     if(keys.length>1){
        if(keys[1].indexOf("paramApple")>-1)
    		url="//www.google.com";
    	else if(keys[1].indexOf("paramMango")>-1)
    		url="//www.yahoo.com";
    	}
    	window.location=url;
    };
    </script>
    
    <div><h3>You will be redirected in <span id="time">00:05</span></h3>

    Wednesday, January 18, 2017 6:51 PM
  • User-271186128 posted

    Hi

    how can I add countdown to this redirection code?

    You could modify your code as below:

        <script type="text/javascript">
            function startTimer(duration, display) {
                var timer = duration, minutes, seconds;
                var end = setInterval(function () {
                    minutes = parseInt(timer / 60, 10)
                    seconds = parseInt(timer % 60, 10);
    
                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;
    
                    display.textContent = minutes + ":" + seconds;
    
                    if (--timer < 0) {
    
                        var keys = (window.location.search || "").split("=");
    
                        // http://localhost:22620/WebForm29.aspx?a=paramMango
                        var url = "//www.bing.com";
                        if (keys.length > 1) {
                            if (keys[1].indexOf("paramApple") > -1)
                                url = "//www.google.com";
                            else if (keys[1].indexOf("paramMango") > -1)
                                url = "//www.yahoo.com";
                        }
                        window.location = url;
                        clearInterval(end);
                    }
                }, 1000);
            }
    
            window.onload = function () {
                var fiveMinutes = 5,
                    display = document.querySelector('#time');
                startTimer(fiveMinutes, display);
            };
        </script>
    
                <div>
                    <h3>You will be redirected in <span id="time">00:05</span></h3>
                </div>

    Best regards,
    Dillion

    Thursday, January 19, 2017 6:07 AM
  • User-851246726 posted

    Hi Dillion,

    Thank you so much for the update. That's exactly what I was looking for.

    One more small request. I would like to keep everything same but just add a button that says "Click to continue" .

    The button basically will speed up the process and users who do not want to wait for the countdown will be able to skip the process and continue with the destination page.

    "Continue" button will still take the user to the destination location as per the URL parameter.

    Please help with the code?

    Thursday, January 19, 2017 7:36 PM
  • User527778624 posted

    Hi,

    One more small request. I would like to keep everything same but just add a button that says "Click to continue" .

    Try this code:

      <script type="text/javascript">
            function startTimer(duration, display) {
                var timer = duration, minutes, seconds;
                var end = setInterval(function () {
                    minutes = parseInt(timer / 60, 10)
                    seconds = parseInt(timer % 60, 10);
    
                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;
    
                    display.textContent = minutes + ":" + seconds;
    
                    if (--timer < 0) {
                        redirect();
                        clearInterval(end);
                    }
                }, 1000);
            }
    		function redirect(){
    			var keys = (window.location.search || "").split("=");
    			var url = "//www.bing.com";
    			if (keys.length > 1) {
    				if (keys[1].indexOf("paramApple") > -1)
    					url = "//www.google.com";
    				else if (keys[1].indexOf("paramMango") > -1)
    					url = "//www.yahoo.com";
    			}
    			window.location = url;
    		}
    
            window.onload = function () {
                var fiveMinutes = 5,
                    display = document.querySelector('#time');
                startTimer(fiveMinutes, display);
            };
        </script>
    
    	<div>
    		<h3>You will be redirected in <span id="time">00:05</span></h3>
    		<br/>
    		<input type="button" value="Click to continue" onclick="redirect()" />
    	</div>

    Friday, January 20, 2017 6:24 AM