2

I want to tag my mobile visitor with a specific URL like http://www.example.com.index.html?source=mobile i used this script to redirect visitors to the given url

<script type="text/javascript">
<!--
if (screen.width <= 800) 
{
    window.location ="http://www.example.com/index.html?source=mobile";
}

//-->
</script>

But when i used this script the page kept on loading again and again so i tried to add another condition in this script but i am confused how to make the second condition to stop loading again and again.

<script type="text/javascript">
<!--
if (screen.width >= 800) 
{
    window.location ="http://www.example.com/index.html?source=mobile";
}
 Else if (document.referrer ="http://www.example.com/index.html?source=mobile")
{
    //stop execution here
}


//-->
</script>

Now plz someone help me to stop execution of javascript if second condition is true.

Thanks in advance.

3
  • just use return statement. Commented Aug 3, 2013 at 12:27
  • 2
    the second condition will never be met because the first one will have already redirected see my answer below. Thanks Commented Aug 3, 2013 at 12:48
  • I've just written you a JS code below, I am not saying this is the perfect you can get but it may work - I've tested it in my Chrome JS Console. Commented Aug 3, 2013 at 17:18

5 Answers 5

2

Wrap the statement in an anonymous function:

(function(){
    if (screen.width >= 800)  {
        window.location = "http://www.example.com/index.html?source=mobile";
    }else if (document.referrer == "http://www.example.com/index.html?source=mobile") {
        return;
    }
});

You can only use return from within a function, despite what the other answers claim.

I am also assuming that you will be adding more code beneath the if statement, otherwise there is no use in stopping execution.

A few issues with your code:

  1. You spelt else with a capital E and is should be lowercase
  2. in the else if statement you didn't use two equals

Both issues amended in the above code

Sign up to request clarification or add additional context in comments.

1 Comment

return false will be more handy
1

how about

if (screen.width >= 800 && document.referrer !="http://www.example.com/index.html?source=mobile") 
{
    window.location ="http://www.example.com/index.html?source=mobile";
}

The way you have it set up now will not work as the first if will always be met, you should either do as I suggest here or put the document referer condition first (plus you need == not =).

2 Comments

@lededje I explained why trying to terminate the script wouldn't solve his issue of constant reloads of the page and this solves that so what exactly is your point?
This answer is totally relevant, but it's not a very good solution. I tried this but it seems that Google Chrome Javascript engine continues the execution until the new page data is returned from the http channel.
1

you should detect mobile browsers directly inside php/whateveryouuse, there's a lot of mobile detector scripts (checking the user agent how to check if the request came from mobile or computer in php)

the way it's now: the use enters the site, if the resolution is low he is redirected to... the same page! (loaded the site twice)

if you detect mobile serverside: the user enters the site only once <-- faster and works even if javascript is disabled

also, you should read something about responsive designs if you only care about the browser's resolution

Comments

0

use window.location.href to get the URL

And you have a typo:else if.. not Else if

 if (screen.width >= 800 && window.location.href !="http://www.example.com/index.html?source=mobile") 
{
    window.location.href ="http://www.example.com/index.html?source=mobile";
}

In context of stoping the execution,

else if (window.location.href ="http://www.example.com/index.html?source=mobile")
        {
            return;//to stop execution here..use return;
        }
   // This condition is unnecessary though

NOTE:There is a difference between return true,return false and return

return false: stops the event from "bubbling up".Halts the execution

return true:continues the execution.

return;

Here is a quote from the ECMA 262 standard, which JavaScript is based upon, regarding return; As noted the result is "undefined".

An ECMAScript program is considered syntactically incorrect if it contains a return statement that is not within a FunctionBody. A return statement causes a function to cease execution and return a value to the caller. If Expression is omitted, the return value is undefined. Otherwise, the return value is the value of Expression.

2 Comments

Capital E in else? SyntaxError: Unexpected token if. return not inside a function? SyntaxError: Illegal return statement
Also, setting document.referrer in an if? (I know it's copypasta)
0

IE8, IE8+, IE11, Firefox, Chrome:

<script>
  ... I wana cancel here ...
  if (document.execCommand) document.execCommand('Stop');   
  if (window.stop) window.stop();   
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.