consider I have an array with some urls:
$array[0]='mywebsite/pagea';
$array[1]='mywebsite/pageb';
$array[2]='mywebsite/pagec';
//> please note this is the PHP array, 开发者_高级运维but I can output as a javascript array without problems
I will output them in my link.php
Is there a way with jQuery to read that fragment (idX) and then redirect to the corrispondent url?
Edit
Thinking about compatibily (browsers without javascript) now let's consdier I have this kind of link:
<a href="mywebsite/pagea" class="intercept">pagea</a>
<a href="mywebsite/pagea" class="intercept">pageb</a>
<a href="mywebsite/pagea" class="intercept">pagec</a>
For browser without javascript the link will work as normal, for every else i will do like
$('.intercept').onClick( function (){
//> append the hashtag to the current url
//> make the right redirect after some interval (ie this.href)
//> how? xD
});
At this point i have only to check if the url was opened with an hashtag and make the right redirect.
I misread your original question. Sorry.
Assuming $array is a javascript array or object.
$('a').click(function(){
var url = $(this).attr('href'),
id= parseInt(url.split('#id')[1]);
window.location = $array[id];
});
If you want to trigger the redirect even if someone visits the page with a hash, you want to use a hashchange event. The jQuery bbq plugin makes this easy to do cross browser with its hashchange event.
It is a pretty lightweight plugin so I doubt there's going to be much advantage to coding up something on your own that will work cross browser and you would likely end up with similar code to the bbq plugin.
精彩评论