Saturday 6 January 2018

javascript - How do I modify the URL without reloading the page?

itemprop="text">

Is there a way I can modify the URL of
the current page without reloading the
page?




I would like to access the
portion before the # hash if
possible.



I only need to change the portion
after the domain, so it's not like I'm violating
cross-domain policies.




window.location.href = "www.mysite.com/page2.php"; // Sadly this
reloads

class="post-text" itemprop="text">
class="normal">Answer



This can
now be done in Chrome, Safari, Firefox 4+, and
Internet Explorer 10pp4+!




See this
question's answer for more information:
href="https://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page">Updating
address bar with new URL without hash or reloading the
page



Example:




function processAjaxData(response, urlPath){

document.getElementById("content").innerHTML = response.html;
document.title
= response.pageTitle;

window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"",
urlPath);

}



You can
then use window.onpopstate to detect the back/forward button
navigation:



window.onpopstate =
function(e){
if(e.state){

document.getElementById("content").innerHTML = e.state.html;
document.title =
e.state.pageTitle;

}
};



/>

For a more in-depth look at manipulating browser
history, see href="https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history"
rel="noreferrer">this MDN article.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...