Reputation: 89
I'm doing an Ajax site and I must add to all urls "#!" house (does not work for external urls) from my site when a visitor clicks on the url. For example when someone clicks on a url http://mysiteajax.com/contact/ ", the script changes the url like this:" http://mysiteajax.com/#!/contact / "
I have coded a little script jQuery but it does not work:
<script>
var base_url = "http://localhost/ajaxsite/";
function link(href) {
// Check if the URL is an internal url
if(href.indexOf(base_url) !=-1 || href.indexOf('http://') == -1 || href.indexOf('https://') == -1) {
href = href.replace(base_url,'');
return base_url + '#!/' + href;
}
}
// Changes the link when someone clicks
$(document).ready(function () {
$('a').click(function() {
$('a').attr('href', link(this));
});
});
</script>
Can you help me?
Thank you
Upvotes: 1
Views: 125
Reputation: 20215
Here are some problems that I see:
If I assume that you have ajax somewhere, then this will still not work for reasons 2 and 3 (assuming that your ajax request should be made to the root)
Change this:
$('a').attr('href', link(this));
to this:
$(this).attr('href', link($(this).attr('href')));
This is a little messy, but it's the only way to only change the clicked element using only jQuery-isms.
I don't see any reason to doing it the way you are doing it. If you're looking for this, I don't think Google's web crawlers interpret JS (that would take way too long on the scale they do their crawling). You'll need to put this in the HTML itself.
Upvotes: 2