Note that there are some explanatory texts on larger screens.

plurals
  1. POPass hash value to Jquery .load() function
    primarykey
    data
    text
    <p>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:</p> <p><code>&lt;a href="#targetpage.html" onclick="menu('targetpage.html');"&gt; TARGET PAGE&lt;/a&gt;</code></p> <p>That calls this very simple function:</p> <p><code>var menu = function(page){ $('#article').load(page); //other code };</code></p> <p>It works fine.</p> <p>However, I want the user to be able to use permanent links such as <code>www.mysite.com/index.html#targetpage.html</code> and I expect the js to read the hash value in the URL and populate the div accordingly.</p> <p>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.</p> <p><strong>EDIT</strong> If I try</p> <pre><code>var thispage = document.location.hash; var hash = thispage.replace( /^#/, '' ); </code></pre> <p>and type mysite.com/#page_one.html, the alert says:</p> <pre><code>page_one.html, </code></pre> <p>which is fine!!! But then</p> <pre><code>menu(hash); </code></pre> <p>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:</p> <pre><code>&lt;head&gt;&lt;script type="text/javascript" charset="iso-8859-1"&gt; var thispage = document.location.hash; var hash = thispage.replace( /^#/, '' ); alert(hash); menu = function(page){ $('#article').load(page); }; menu(hash); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="menu"&gt; &lt;ul&gt; &lt;li&gt;&lt;p class="menuvoice"&gt; &lt;a href="#page_one.html" onclick="menu('page_one.html');"&gt; Page One&lt;/a&gt;&lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p class="menuvoice"&gt; &lt;a href="#page_two.html" onclick="menu('page_two.html');"&gt; Page Two&lt;/a&gt;&lt;/p&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div id="article"&gt;&lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload