Tuesday 26 June 2012

Navigate or redirect to a url using javascript

Problem:

How do I use javascript to navigate to another url?

Solution:

The follow are two ways that can redirect a user to another page:

// redirect without updating browser history
window.location.replace("my-next-page.html");

In the above and below example "my-next-page.html" should be replaced with your own URL. The above example shouldn't update the change of pages in the browser history, whereas the next example will.

// redirect and update in browser history
window.location.href = "my-next-page.html";

Notes:

This was tested on the iOS 5.1 browser, Firefox 12, and with PhoneGap 1.5.0 on iOS 5.1. This should also work with most other browsers.

No comments:

Post a Comment