Skip to content Skip to sidebar Skip to footer

How Do I Run A Function After A Calling Window.location.href?

I am trying to redirect to another site then run code on it. function gotonewpage() { window.location.href = 'WEBSITE' x = document.getElementById('s') }

Solution 1:

It's not possible unless you control that site you're redirecting to.

Solution 2:

As noted you must control the other page. If it is on the same site, it is possible to open it in a frame | iframe | new window and javascript can communicate to that frame, but you can not close the current window, (the script goes away when the window is called). If the domain does not match "Exactly" it generates an error. You can not communicate between domains and subdomains of that domain.

Your code does the following:

functiongotonewpage() {
  window.location.href = "WEBSITE"// current window with script goes away.
    x = document.getElementById("s") // this code never sees the light of day.
}

This code accesses elements of a iframe on same domain.

<iframe id="frame" src="contents.htm"></iframe>

   MySite=document.getElementById("frame");
   x = MySite.contentDocument.getElementById("s");

for a popup commutation is via messages.

MyChildMySite = window.open("website");
   MyChildMySite.ProcessParentMessage('Message to the child');

On the Child page, same domain

functionProcessParentMessage(message) {
    window.opener.ProcessChildMessage(document.getElementById("s"));
}

And Back on the Parent page, same domain

functionProcessChildMessage(s) {
    x = s;
    // not sure what you want to do from here.
}

Let me add browsers are serious about security; If you can find a security error in their implementation contact them for a possible reward.

Solution 3:

Well, window.location.href reloads the application and the rest code never get chance to execute it.

It's like using statement after return keyword in the function.

functionsum(a, b) {
   var sum;
   returnsum;

   sum = a + b; // this will never execute
}

But what you can do it, After the redirection takes places, you can add this

x = document.getElementById("s")

In the file where redirection took place. But I'm afraid you have to add element with id 's' in your new file too.

Solution 4:

I'm afraid that's not possible, I think the only way to do so is through a reflected xss (assuming this is not your site)

Post a Comment for "How Do I Run A Function After A Calling Window.location.href?"