I have a website with a navigation bar. When a link is clicked, it uses the
onClick() handler to change the content of a page.
For example. If the about us link is clicked, the page content will change to the about us content, and the URL will become
xxxxx.com/#about. However, when linked directly to
xxxxx.com/#about it opens the default
How do I directly link to the pages, as if it was
preguntado el 27 de agosto de 11 a las 22:08
You can call the function used in onclick on page load.
will give you #about in your example, so you can load(or show) the relevant content.
You need get the
#something on page load, and then if it is found, call your
onclick funcionar con él.
tag = /#[^\s]+/.exec(document.location.href); tag = tag.substring(1); // you have what you want
so you're loading in content with Ajax?
A quick and dirty way to do this would be to look at the url
You can get the action you're looking for like this
// get the url and split it at the "#" character into an array var urlSplit = document.location.href.split("#"); // the 2nd item in the array will be the page var page = urlSplit;
now you know the 'page', you can call whatever function that loads the page content
The #about is just pointing to a section within a page, not actually changing page. I'm guessing the onClick event refreshes the page with AJAX, and updates the URL shown to the user by changing document.location or similar.
Generally, I would say don't use AJAX for navigation. If you're changing to a different page, let the browser do a proper update, so the back button will work as the user expects it to. Use AJAX when you're dynamically altering portions of a page.
If you really want AJAX navigation, use window.location.hash, as suggested by strada.