0

How do I direct a user to another page if the if statement comes true?

For example, the javascript below. If the login details are correct, I want the user to be directed to another page of the website, for example "#page5".

The code is what I currently have which only notifies the user and then redirects back to my index page. However, I want to direct it them to a specific page of the app.

function renderList(tx, results) {
  if (results.rows.length > 0) {
    navigator.notification.alert('Login, Success!');
  } else {
    navigator.notification.alert('Incorrect! Please try again.');
  }
}

Thanks

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
Mahdi
  • 153
  • 2
  • 16

3 Answers3

2

The global window object contains the document that is currently loaded. It has a property called location that contains the path of the currently loaded resource. Changing this property loads up the resource at the new path into the current window:

   window.location = newURL;

This is such a simple operation that using jQuery would only make it more involved:

   $(window)[0].location = newURL;
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • I should have mentioned that I am using a one page architecture as I using JQueryMobile, so i'm not using seperate html documents. Therefore, how should I adapt the above to this? – Mahdi Mar 14 '16 at 16:05
  • Doesn't matter, just set the location property to the `#section` value that you would have used to navigate to that section if you were to do it manually. – Scott Marcus Mar 14 '16 at 16:06
  • window.location = "#page4"; – Mahdi Mar 14 '16 at 16:27
  • @Mahdi No problem. Don't forget to up vote and mark as answer. Good luck! – Scott Marcus Mar 14 '16 at 16:30
  • I have tried, but it says I have to earn a total of 15 reputation before doing so. Sorry, I'm new to the site so I will up vote once I achieve 15 reputation. – Mahdi Mar 14 '16 at 16:32
  • No worries! Take care. – Scott Marcus Mar 14 '16 at 16:34
1

To redirect in JavaScript:

 window.location = URL;

With jQuery

$(location).attr('href', URL);
PVL
  • 577
  • 1
  • 3
  • 12
1

Use window.location="link url";

This will redirect the current page to the provided url.

Wowsk
  • 3,637
  • 2
  • 18
  • 29