Mark Harrison
Mark Harrison

Reputation: 304434

Javascript: Access address bar and redirect to new URL?

I've been given an old Javascript program that keeps some state information stored in the URL, past the "#" so that the state information is not passed to the HTTP server.

http://example.com/oldprogram/#/some-parms

I need to rewrite the program, so that the parameters are handled on the server. Additionally, I would like to redirect old URLs (saved in email, for example) to the new service.

My question:

How can I open a URL of the old form, and have Javascript rewrite the URL and redirect to the newly rewritten URL? For discussion purposes, translating the "#" to "@" would be perfect.

i.e., clicking on this link:

http://example.com/oldprogram/#/some-parms

causes this page to be loaded:

http://example.com/newprogram/@/some-parms

Upvotes: 1

Views: 1038

Answers (1)

pid
pid

Reputation: 11597

You can use:

window.location = ("" + window.location).replace("#", "@");

This will redirect to the resulting URL as you've requested. Obviously you will have to put this script into the old page.

If this is not an option there's no way you can force a redirect with JS. There are other options like URL rewriting and redirecting through the webserver's configuration.

For Apache, as an example, you would use mod_rewrite.

Upvotes: 2

Related Questions