SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast laresistance2's Avatar
    Join Date
    May 2011
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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:

    Code:
           <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

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This works for me for all links with the exception of a home link

    Code JavaScript:
    	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')) );
    		});
    	});

  3. #3
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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 - I wrote it as a proof of concept)
    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/)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •