Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop execution after changing window.location?

Tags:

javascript

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.

like image 828
Fancy John Avatar asked May 06 '16 13:05

Fancy John


2 Answers

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return;
}
like image 158
Danilo Calzetta Avatar answered Sep 20 '22 10:09

Danilo Calzetta


It's simple: return false...

if (isMobile) {
    window.location.href = '/m/' + window.location.pathname + window.location.search;
    return false;
}
like image 38
Mojtaba Avatar answered Sep 20 '22 10:09

Mojtaba