8

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
  • 36,627
  • 3
  • 24
  • 25

3 Answers3

5
if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return;
}
Danilo Calzetta
  • 1,653
  • 15
  • 31
5

It's simple: return false...

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return false;
}
user664833
  • 16,967
  • 18
  • 88
  • 129
Mojtaba
  • 4,536
  • 4
  • 19
  • 38
-3

There is exit() function implemented in javascript.

How to terminate the script in Javascript

Community
  • 1
  • 1