Jenny
Jenny

Reputation: 919

Cancel a script if directed to that page from a specific URL

I have this bit of code to redirect to a mobile site if the

<script type="text/javascript">
<!--
if (screen.width <= 800) {
window.location = "http://mymobilesite.com ";
}
//-->
</script>

I want to cancel the above script if I am direct to the page form a speific URL, something like:

if (document.referrer !== "http://mymobilesite.com") {

}

Any help would be greatly appreciated

Upvotes: 0

Views: 59

Answers (2)

negative0
negative0

Reputation: 459

If I understand correctly you want to redirect visitors to two different Web Address based on two main categories: 1) Mobile/Tablet 2) Desktops

There are better ways of doing that by using third-party JS plugins to detect visitors device. however, if you still insist on redirecting visitors by window width, please use the code below:

if (window.location.href !== "http://exception-url.com") {
    if (screen.width <= 800) {
        window.location.href = "http://redirected-url.com";
    }
}

Upvotes: 0

Nalin Aggarwal
Nalin Aggarwal

Reputation: 888

If I am not understanding your need, you want to cancel the script, if you are redirected from spcific site. for this, the below will work.

if (window.location.origin !== "http://mymobilesite.com") {
  // do something
}

This does not include any query string, only the url of the website including http/https .

Upvotes: 1

Related Questions