Gianclè Monna
Gianclè Monna

Reputation: 387

Javascript redirect to another page

I have a Javascript function that manages the action done pressing an Input Submit button. I want that, before the function calls return, the user could be redirected to a "mypage.php" in which he will do another action, and, if this action completes successfully, also the Javascript function completes successfully.

Thank you

Upvotes: 0

Views: 69

Answers (1)

Iftah
Iftah

Reputation: 9572

Redirecting to another page in javascript can be achieved by setting window.location;

BUT, when you redirect to another page the code execution of the javascript in the current page ends, a new page is fetched from the server and its javascript starts.

So if you want code on that page to interact with the code on the first page you need a more complex solution. I can think of two options:

  1. The best solution is the do everything in a single-page app. Instead of redirecting to a new page you would fetch data via ajax, render it into a new div, hide the old div. You would have a single source of javascript, no problems.

  2. Another option is to use iframe to show the new page. You can communicate between the page inside the iframe and the page outside it via messages.

Upvotes: 1

Related Questions