Reputation: 29
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.
Upvotes: 2
Views: 46993
Reputation: 1917
IE8, IE8+, IE11, Firefox, Chrome:
<script>
... I wana cancel here ...
if (document.execCommand) document.execCommand('Stop');
if (window.stop) window.stop();
</script>
Upvotes: 0
Reputation: 1869
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:
Both issues amended in the above code
Upvotes: 2
Reputation: 15838
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
Upvotes: 1
Reputation: 17757
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.
Upvotes: 0
Reputation: 944
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 =).
Upvotes: 1