Change the URL in the browser without loading the new page using JavaScript

后端 未结 14 2216
忘掉有多难
忘掉有多难 2020-11-22 02:02

How would I have a JavaScript action that may have some effects on the current page but would also change the URL in the browser so if the user hits reload or bookmark, then

相关标签:
14条回答
  • 2020-11-22 02:38

    I would strongly suspect this is not possible, because it would be an incredible security problem if it were. For example, I could make a page which looked like a bank login page, and make the URL in the address bar look just like the real bank!

    Perhaps if you explain why you want to do this, folks might be able to suggest alternative approaches...

    [Edit in 2011: Since I wrote this answer in 2008, more info has come to light regarding an HTML5 technique that allows the URL to be modified as long as it is from the same origin]

    0 讨论(0)
  • 2020-11-22 02:39

    What is working for me is - history.replaceState() function which is as follows -

    history.replaceState(data,"Title of page"[,'url-of-the-page']);
    

    This will not reload page, you can make use of it with event of javascript

    0 讨论(0)
  • 2020-11-22 02:41

    window.location.href contains the current URL. You can read from it, you can append to it, and you can replace it, which may cause a page reload.

    If, as it sounds like, you want to record javascript state in the URL so it can be bookmarked, without reloading the page, append it to the current URL after a # and have a piece of javascript triggered by the onload event parse the current URL to see if it contains saved state.

    If you use a ? instead of a #, you will force a reload of the page, but since you will parse the saved state on load this may not actually be a problem; and this will make the forward and back buttons work correctly as well.

    0 讨论(0)
  • 2020-11-22 02:42

    With HTML 5, use the history.pushState function. As an example:

    <script type="text/javascript">
    var stateObj = { foo: "bar" };
    function change_my_url()
    {
       history.pushState(stateObj, "page 2", "bar.html");
    }
    var link = document.getElementById('click');
    link.addEventListener('click', change_my_url, false);
    </script>
    

    and a href:

    <a href="#" id='click'>Click to change url to bar.html</a>
    

    If you want to change the URL without adding an entry to the back button list, use history.replaceState instead.

    0 讨论(0)
  • 2020-11-22 02:43

    I was wondering if it will posible as long as the parent path in the page is same, only something new is appended to it.

    So like let's say the user is at the page: http://domain.com/site/page.html Then the browser can let me do location.append = new.html and the page becomes: http://domain.com/site/page.htmlnew.html and the browser does not change it.

    Or just allow the person to change get parameter, so let's location.get = me=1&page=1.

    So original page becomes http://domain.com/site/page.html?me=1&page=1 and it does not refresh.

    The problem with # is that the data is not cached (at least I don't think so) when hash is changed. So it is like each time a new page is being loaded, whereas back- and forward buttons in a non-Ajax page are able to cache data and do not spend time on re-loading the data.

    From what I saw, the Yahoo history thing already loads all of the data at once. It does not seem to be doing any Ajax requests. So when a div is used to handle different method overtime, that data is not stored for each history state.

    0 讨论(0)
  • 2020-11-22 02:47

    There's a jquery plugin http://www.asual.com/jquery/address/

    I think this is what you need.

    0 讨论(0)
提交回复
热议问题