How to add a "#!" before all the urls for ajax
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));
});
开发者_如何学Python });
</script>
Can you help me?
Thank you
Here are some problems that I see:
- There is no ajax in the code snippet
- You are passing an anchor-tag element to link(), not an href
- This will have no effect on page crawlers because crawlers don't execute JS
- The # and anything after it will not be passed to your server
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.
精彩评论