I'm building a basic site where the user clicks a link and Jquery populates a div with id="article" via asynchronous request to the server, using Jquery's load(). This is the html:
<a href="#targetpage.html" onclick="menu('targetpage.html');">
TARGET PAGE</a>
That calls this very simple function:
var menu = function(page){
$('#article').load(page);
//other code
};
It works fine.
However, I want the user to be able to use permanent links such as
www.mysite.com/index.html#targetpage.html and I expect the js to read the hash value in the URL and populate the div accordingly.
The problem is that when I try to pass the value window.location.hash to the function, (on load or on ready state), everything is stuck and my js doesn't work anymore. I also tried with the ajax() instead of load() function, but it doesn't accept the value either.
EDIT If I try
var thispage = document.location.hash;
var hash = thispage.replace( /^#/, '' );
and type mysite.com/#page_one.html, the alert says:
page_one.html,
which is fine!!! But then
menu(hash);
doesn't load the content of the page_one.html into the div. However, clicking on my menu works fine. In the js console I have no errors nor warnings of any kind. Here's the whole code involed:
<head><script type="text/javascript" charset="iso-8859-1">
var thispage = document.location.hash;
var hash = thispage.replace( /^#/, '' );
alert(hash);
menu = function(page){
$('#article').load(page);
};
menu(hash);
</script>
</head>
<body>
<div id="menu">
<ul>
<li><p class="menuvoice">
<a href="#page_one.html" onclick="menu('page_one.html');">
Page One</a></p></li>
<li><p class="menuvoice">
<a href="#page_two.html" onclick="menu('page_two.html');">
Page Two</a></p></li>
</ul>
</div>
<div id="article"></div>
idattribute oftargetpage.html? The fact you have.htmlat the end of the anchor seems strange if this is the case.