How to add a "#!" before all the urls for ajax site

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://myajaxsite.com/contact/ “, the script changes the url like this:” http://myajaxsite.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

This works for me for all links with the exception of a home link


	var base_url = 'your url here';

	function link(oldHref) {
		var href = oldHref.split(base_url)[1];
		if( href !== '' ) return base_url + '#!/' + href;		
	}

	$(document).ready(function() {
		$('a').each(function() {
			$(this).attr('href', link($(this).attr('href')) );
		});
	});

Might be helpful to use a routing framework like Sammy.js (http://sammyjs.org/) or Crossroads.js (http://millermedeiros.github.com/crossroads.js/)

I built a little sample a while back (please don’t judge my code :stuck_out_tongue: - I wrote it as a proof of concept)
[URL=“http://afterlight.com.au/lab/routing/”]http://afterlight.com.au/lab/routing/

If you just want access to the history state you could use History.js (https://github.com/balupton/History.js/)