0

I have an ajax method which submits data over ajax. The server then responds with a URL to load. Most times the URL will be different to the one on which the request was made, but sometimes the URL is the same.

Either way, I want the page to go to the intended url, even it's just reloading the page.

I've tried the window.location.href = '' method, but if the current url is the same as the intended url, nothing happens.

I've tried the window.location.replace(url) method, but that doesn't reload the page if current === intended.

I've tried the window.location.reload(true) method, but that's only good if the current url is the same as the intended url.

I've tried to compare the intended URL to the current url and do either a reload or a href method, but this has problems as well. If I click on a bootstrap tab, the hash doesn't appear in the URL, so the current url value is actually incorrect.

For your reference, this is/was my comparison:

if(window.location.href === response.intended) {
    window.location.reload(true);
} else {
    window.location.replace(response.intended);
}

All I want to do, is force the browser to redirect to an intended URL, or reload the current page depending on current URL vs intended URL.

6
  • 2
    I've tried the window.location.href = '' method, but if the current url is the same as the intended url, nothing happens. Are you sure ? Commented Oct 26, 2015 at 14:15
  • 1
    is there any # character in the url? Commented Oct 26, 2015 at 14:17
  • @rajuGT sometimes there will be, i'm using bootstrap tabs. Commented Oct 26, 2015 at 14:19
  • Try this with current SO question URL. with # the page is just moving to the post. but if you remove that #, its reloading the whole page. Try and check the difference. This might be the issue I guess. Commented Oct 26, 2015 at 14:21
  • @RayonDabre At the moment, it's definately not doing it when there's a hash in the url (for bootstrap tabs), for example, window.location.href = someurl.com/test.php#tab_1 doesn't reload / redirect the page. Commented Oct 26, 2015 at 14:21

3 Answers 3

3

To ensure your page isn't being cached, add a random number to the end of the url:

window.location.replace(response.intended + "?rnd" + Math.random() );
Sign up to request clarification or add additional context in comments.

1 Comment

Good idea, didn't think of this approach! Thanks for an answer :)
1

Add window.location.hash to the new url for comparison. If there isn't one it will return an empty string

if(window.location.href === response.intended + window.location.hash) { 

Comments

0

As discussed over comments, I think this is due to the # character in the url, which makes the browser to move to particular section of the page like <a href='33347913'> tag.

var index = response.intended.indexOf('#');
if(index >= 0) {
    response.intended = response.intended.substr(0, index);
}
window.location.href = response.intended;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.