11

Say I have some mobile check at the very top of my script:

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
}

And I also have some metrics down the code (or even in some other module that is loaded asynchronously) that are being sent to my metrics server.

My question is: how can I stop execution of everything that goes below my window.location change? Or even better, how to stop the whole javascript engine so that it doesn't execute any other asynchronously loaded modules?

I know that I can do mobile proxying in many different ways, but in this task my mobile redirect must be run on the client side, it is a given.

Fancy John
  • 38,140
  • 3
  • 27
  • 27

3 Answers3

7
if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return;
}
danilonet
  • 1,757
  • 16
  • 33
6

It's simple: return false...

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return false;
}
user664833
  • 18,397
  • 19
  • 91
  • 140
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
-3

There is exit() function implemented in javascript.

How to terminate the script in Javascript

Community
  • 1
  • 1