Using JavaScript to change the URL used when a page is bookmarked

后端 未结 6 607
误落风尘
误落风尘 2021-01-14 05:55

JavaScript doesn\'t allow you to update window.location without triggering a reload. While I agree with this policy in principle (it shouldn\'t be possible to

6条回答
  •  情深已故
    2021-01-14 06:01

    You can add   [Add to Favorites]   button on the page.

    Sample:

    var urlAddress = "http://www.example.com/#image1";
    var pageName = "Example Page Title - Image1";
    function addToFavorites() {
        if (window.external) {
            window.external.AddFavorite(urlAddress, pageName); 
        } else {
            alert("Sorry! Your browser doesn't support this function."); 
        }
    }
    

    Or use one of these jQuery plugins:

    • http://plugins.jquery.com/project/bookmark
    • http://plugins.jquery.com/project/jqbookmark
    • http://plugins.jquery.com/project/AddFavourite
    • http://plugins.jquery.com/project/jFav
    • http://plugins.jquery.com/project/jBookmarkEngine

    AND / OR

    Use URLs with hash at the end and load your content (images etc.) based on that hash value.

    function onLoad() {
        if (window.location.hash == "image1") {
            // load image1
        }
    }
    

    There are also lots for jQuery plugins for working with URL hash events, for example:

    • http://plugins.jquery.com/project/hashhistory
    • http://plugins.jquery.com/project/hashchange

    There are also lots of non jQuery JavaScript libraries for that, for example:

    • http://code.google.com/p/reallysimplehistory/

提交回复
热议问题