how to change an URL with javascript so i can link to that URL
I'm making a gallery with jquery. when i press a button on my page it fades out and the gallery should fade in. Can i change the url at the top so when a user copys the url and uses it somewhere else it opens up the gallery and not the first page (it's actually all in one page i would o开发者_运维问答nly use the fade in and fade out functions to navigate trough different content) i googled every possible word sentence and didn't find anything so i would be relly thankful if someone could tell me the answer :)
When your button is clicked, add following line to your process function:
window.location = "#gallery"
It adds an anchor name to the browser's URL.
Then when the page is loaded, you should get this anchor name and show the right content:
var location = document.location.href;
var tabName = location.substring(location.indexOf('#') + 1);
showTab(tabName);
What youre looking for is commonly found in googles gmail. It uses anchors in URL (host/path?query#anchor) to detect previous location.
Because browsers do not send anchors to servers you cannot send the appropriate webpage back. This is done on the client, so you have to query an update from your server via ajax.
Anyway...
Step 1: Use anchors to navigate on your website ( a href="#mypage1" or updating the location property )
Step 2: When your index page loads check for anchors with javascript (property window.location.hash http://www.w3schools.com/jsref/obj_location.asp)
Step 3: Request an update from your server via ajax and parse it when it arrives
I suggest you use json for content payloads. Easy to use, better than xml, much better than html (if you are tempted to use the innerhtml property). This is the most coplicated part.
Oh, you could just send the whole website and than just switch rather than loading via ajax, but I think people like the described way more.
Some links I found just now:
http://www.floatnotes.org/documentation/about-locations http://blog.rebeccamurphey.com/2007/12/04/anchor-based-url-navigation-with-jquery
You'd have to add parameters to your url query like www.yourdomain.com/index.html?openGallery=true or www.yourdomain.com/index.html#openGallery=true
Then using JS, on page load, have some code to check for those url parameters and launch your gallery.
精彩评论