My code has a delay in loading a file. I have to click <a href="#string" id="hash"></a>
twice to make it show the .txt file.
The code:
<script type='text/javascript'>
$(document).ready(function(){
$("a#hash").click(function(){
var loc = $(window)[0].location;
var getUrlString = $(loc).prop('hash').substr(1);
$("p").load(getUrlString + '.txt');
});
});
</script>
<body>
<p>This is a placeholder for your content</p>
<a id="hash" href="#login">Login</button><br />
<a id="hash" href="#register">Register</button><br />
<a id="hash" href="#recovery">Recovery</button>
</body>
For example if i click login the first time the page has loaded – it wont show anything.
If i click Register after clicking Login – it will show Login.
If i click Recovery after clicking Register – it will show Register, but if i click Recovery again it will finally show Recovery.
Sorry for my bad english, or if it’s hard to understad what’s my problem. I’m still learning jQuery, and i find it easier to learn if i try/fail myself, and then find out what’s the problem. By just reading tutorials it’s not the same 🙂
Thanks.
First of all, the
IDshould never be repeated within your page. If you need to group elements by an identifier, use a class.Secondly the problem with the method you have now is that it will read the URL of the window BEFORE it changes to reflect the new URL in the
awhich was clicked. This is why you’re seeing the content from the click before appearing. If you need to get the hash value of the URL, it is much easier and simpler to get it from thehrefattribute of the link which was clicked.With that in mind, try this:
Example fiddle