Can JavaScript which is not part of a userscript-manager run after redirecting the user?

Primarily for the sake of learning I would like to redirect myself to another webpage and then, in that new webpage, get an alert() message.

Can I do that with a single JavaScript document?
I mean, can I run both commands from the same document?

In a glimpse, I understand that I can’t, because if one runs the following code, the alert() will run before the setTimeout() and not after it in the new webpage (which I find a bit odd because the window is the same window and only the webpage was changed).

window.setTimeout( () => {
  window.location.href = 'https://algeriatimes.com';
}, 2500);

alert("Hi");

Is there some kind of a JavaScript “inheritance” command that could make the alert() run in the new webpage?

I thank dearly for any help with this.

Hi,

You want to redirect from page A to page B. After the redirect has taken place and page B has loaded, you want to execute JavaScript from page A in the context of page B.

Did I get that right?

Yes. indeed, I want the alert() from page A to run in page B.

That’s not possible, I’m afraid. If we are talking about a full page refresh (and not some kind of JS-based routing) then page B has no concept of page A.

You could hack around this by passing a query string to page B, but that’s probably not what you are trying to do.

Nope, no query strings.

Isn’t there a work around with browser session or something similar?

Local storage maybe?

I gotta run, but check this out.

In that QA session:

  • Guffa said:

There are different ways of persisting data, like in the query string, post data, cookies, window name or HTML5 local storage, but all those methods can only persist string values, not Javascript objects.

  • freakish said:

I believe that the only way to pass a javascript object from one page to another is to serialize it into string and pass it in url.

  • thomasrutter mentioned “local storage”.

  • Also based on answer by Robert Koritnik’s, I guess local storage or cookies are good ways to do that.

Sadly non of the answers there include an example of actual local storage of a command.
Would the command be locally stored as a string? Let along, how this command should be POSTed(?) into the next page.

Is there any tutorial here which I might take in these subjects? I basically just want to know how to pass the alert() to the next page.

Right. You would store a string of alert("Hi"); in local storage on page A, then on page B, retrieve the string from storage and use eval to execute it.

However, this is a security risk and likely a bad idea (see the warning on the MDN page). While what I described should work, there is nothing to stop a bad actor adding random JavaScript to local storage on page A and then having it executed on page B.

It would be better to pass a query sting in the URL, e.g. https://whatever.com/page_b.html?message=hi and then parse the query string on page B and alert the message.

And even if alerting a message is just a “dummy” command and you are trying to do something more complicated, there is very probably a better way to do that as well.

Right. This is how JS works. The setTimeout is non blocking so the JS interpreter will not wait for the timer to run before executing the alert.

Also, any code which comes after window.location.href = 'https://algeriatimes.com'; will not be executed, as you will have navigated away from the page you were on.

HTTP is a stateless protocol which means that each request made from a client to a server is processed independently of any other request. In other words, the server doesn’t remember anything about the client between different requests.

1 Like

First, I thank you for this generous detailing.

And even if alerting a message is just a “dummy” command and you are
trying to do something more complicated, there is very probably a better way to do that as well.

Do you mean that there is a better way even than query strings?

HTTP is a stateless protocol which means that each request made from a client to a server is processed independently of any other request. In other words, the server doesn’t remember anything about the client between different requests.

I think that between different requests you probably meant between different backend requests.


Let’s say that instead of local storage, string and eval I do it with query strings.
Should I pass any query string to the next webpage by XHR/FetchAPI/AJAX?

As I only want to pass alert() I guess any elementary tutorial with XHR/FetchAPI/AJAX would suffice, I just don’t know which has the shortest syntax or best way to pass on query strings.

Not really. I mean that what you are trying to do (execute JavaScript from page a in the context of page b) is not possible. However, it might be that you have a different use case in mind and that that could be solved in a better way than what you are trying to do.

You are making a backend request.

window.location.href = 'https://algeriatimes.com';

This makes the browser load the page located at https://algeriatimes.com. It requests this page from a server, therefore a “backend request”.

Nope. window.location.href will do a full page refresh. No Ajax involved.

Here is an article on how to work with querystrings:

But TBH, it seems like you are mixing your concepts a little (or something is getting lost in translation). I would recommend giving yourself a brief refresher of what happens where. For example:

1 Like

Hi James !

I took a moment to think about this:

it might be that you have a different use case in mind and that that could be solved in a better way than what you are trying to do.

I assume the same thing :slight_smile:

What I was thinking about is just creating a script which runs on all the webpages of my personal website but has two parts instead just one.

  • The first part deals only with a page which is an edit mode page.
  • The second part deals with all pages which are not an edit mode page.

Each part will have a URL test such as:

if (window.location.href.includes() ) {
    do stuff;
} else if (window.location.href.includes() ) {
    do stuff;
}

I am not sure if I would also need an optionality operator here, such as:

if (?.window.location.href.includes('')) {}

Depending on the length of the script you could also write two separate userscripts and have one execute when the URL matches the first pattern and the second execute when it matches the second pattern.

Makes sense.

That can’t go at the front.

It would be something like:

window?.location?.href

But the window is always going to have those properties, so it is not necessary.

James please one more thing.

if (window.location.href.includes(/[0-9][0-9][0-9]/edit) ) {
    do stuff;
} else if (!window.location.href.includes(/[0-9][0-9][0-9]/edit) ) {
    do stuff;
}

I have hundreds of webpages in my personal website and currently all webpages are page/200 or page/305 or page/716 etc. in the URL, so given that datum, is it better to do it this way with regex and negation (I mean !window instead just window)?

What patterns are you trying to match?

Obviously page/200 (i.e. “page” followed by three digits), but what else?

The patterns are as follows.

Non-edit mode:

example.com/node/great-gardening-tips-for-growing-fruit-bearing-trees-in-scotland

Edit mode:

example.com/node/NODE_NUMBER_LIKE_100_OR_1000_OR_10000/edit

So is it the case that:

  • you want to run your script for every page of your site
  • you want to do one thing for normal pages and a different thing for edit pages

Or are there some pages on your site where the script shouldn’t run?

Indeed that’s the case.
No other type of pages appear in my mind, so nope, currently there are no other pages on which the script shouldn’t run.

Can you not then do:

if (window.location.href.match(/node\/\d+\/edit) ) {
  // Edit pages
  do stuff();
} else {
  // Normal pages
  do otherStuff();
}
1 Like

I should try that ! :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.