Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning to document.location.href without clobbering history

Tags:

javascript

In testing document.location.href, I have observed that when the user initiates an action that results in javascript that assigns to document.location.href, the new URL is added to the history.

However, if the call is initiated by javascript that is result of, say, state change of an XMLHTTPRequest, the entry for the current page in the history is over-written. Have I characterized this correctly? Is there a way to get the page change to be reflected in the history in this latter case?

like image 518
wolffiex Avatar asked May 14 '09 17:05

wolffiex


People also ask

How do you pass the message in window location href?

Using window. location. href it's not possible to send a POST request. What you have to do is to set up a form tag with data fields in it, set the action attribute of the form to the URL and the method attribute to POST, then call the submit method on the form tag.

Does window location href reload the page?

location. reload() reloads the current page with POST data, while window. location. href='your url' does not include the POST data.


1 Answers

URL can be manually added to history before redirecting the user.

if (window.history) {
    history.pushState({}, window.location.href);
}
window.location.replace("/login/?next=" + window.location.pathname);
like image 100
xtranophilist Avatar answered Oct 10 '22 13:10

xtranophilist