1

I would like build a customs predefined java script that will performed a series of actions On 2 different websites.

However my issue is that when I use a redirect

window.location.href

The next command in the script will not be performed. I am using Tampermonkey extension to perform the script For example I’m using the following script

however, the script will never get to the second alert I asked a similar question on the following thread

Simulate mouse clicks in JavaScript in Chrome

How can I work around the window.location.href?

(function() {
    'use strict';

    alert('alert1');

    window.location.href = ("http://www.w3schools.com");
    alert('alert2’);
})();
JJJ
  • 32,902
  • 20
  • 89
  • 102
starlev
  • 57
  • 1
  • 6
  • 1
    Assigning to `window.location.href` should be the last thing you do. Can you just move the alert to before? – CertainPerformance Jul 12 '19 at 08:32
  • 2
    You cannot perform anything after `redirection`, because the script you are running is no longer available to the targeted URL. You should do whatever you want to perform before this line only. – ravibagul91 Jul 12 '19 at 08:34
  • You can try delaying the redirect, or using `onbeforeunload` event – V.Volkov Jan 11 '20 at 17:02

2 Answers2

2

You cannot do this with scripts running on the browser. Once a navigation occurs, the current JavaScript is no longer valid.

You will need to have control over the client and need persistence between page loads.

Following are a few options (in no specific order)

  • Browser Extension (I'm not very familiar with them, so you will need to do research)
  • Simulate HTTP requests
    • You may write a small application that would do the corresponding HTTP requests (possibly handling cookies, redirects .etc.) to accomplish this. If you write in C#, I recommend WebClient and HTML Agility Pack
    • This won't work if you are expecting to run the site's JavaScript.
  • Write a script to run a headless browser solution such as headless chrome. You can likely also use UI testing tools like Selenium as well.
  • Electron (pretty much Chrome's rendering and JS engine stripped from the rest for building cross platform apps, but you can use it accomplish your goal.)
halfer
  • 19,824
  • 17
  • 99
  • 186
Madushan
  • 6,977
  • 31
  • 79
1

By default, the way window.location works is that it simply tell the browser to redirect to the given URL. Any code after that is basically non-existent for the browser. Why do I say that? Because the code can run only until the point that the control is given to the browser. The moment the new URL is being fetched, the code execution stops.

So, to answer the question - Does code execution stop after a window.location.href -> Not really, but yeah!

Think of it as a return statement of sorts. You perform some operations, and then you return to another page (using window.location). Your alert is probably being executed, but the browser starts the redirect before it can be displayed.

You can try switching to console.log's to confirm the same, as well as enable 'Persist Logs' in the browser devtools.

However, I've had a similar problem in the past, and I solved it by following some Stack Overflow answers.

So, I'm going to link the answers here so that they'll get the credit for the actual solutions -

jQuery code not called after window.location.replace( url )

How to run code after changing the URL via window.location?

This might help too (if you're new to JS) - Javascript: Detect when a window is fully loaded

halfer
  • 19,824
  • 17
  • 99
  • 186
BkiD
  • 576
  • 3
  • 10
  • Thank you for your tips. I am working with combining sessionStorage together with Tampermonkey. Hopefully it will work. – starlev Jul 15 '19 at 12:45