0

I need to remove the # from my URL so that the completed URL is example.com/sub-page-title instead of example.com/#sub-page-title.

I was able to get a repetative URL to go away using substr(31), 31 being the length of the URL including http://... but I'm sure that is not the ideal way to do this.

NOTE: I cannot have a page refresh.

On click, an external page is loaded into a div (which then slides into place)...once that is visible, I want the URL to reflect this (without #). That way going forward and hitting "Back" will bring the users to the actual page.

CODE:

    $(document).ready(function(){

    var hash = window.location.hash.substr(1);
    var href = $('a.load').each(function(){
        var href = $(this).attr('href');
    });


    $('a.load').click(function(){

    var toLoad = $(this).attr('href')+' #project-details';
        $('.wide-frame').animate({
            left: -985,
            duration: 'slow'
        })
    $('#project-details article').hide();   
    $('#project-details').fadeIn('normal',loadContent);


    window.location.hash = $(this).attr('href').substr(31);

    function loadContent() {
        $('#project-details').load(toLoad,showNewContent)
    }
    function showNewContent() { 
        $('#project-details').show('normal');
        $.getScript("<?php bloginfo('template_directory'); ?>/scripts/ajax-control.js");
    }
    return false;

    }); 

});

I don't know if this is pertinent, but I am also using Hash history with jQuery BBQ for some other navigation features.

Cœur
  • 37,241
  • 25
  • 195
  • 267
RevConcept
  • 253
  • 1
  • 6
  • 19
  • I believe what you want to do has already been addressed in [this thread](http://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page). – Jacob VanScoy Mar 13 '13 at 21:25

1 Answers1

0

How about a nice little RegExp ?

Otherwise just use the replace() method.

like this :

var string = "url/#hash";
string.replace('#','');
Romain Braun
  • 3,624
  • 4
  • 23
  • 46
  • 1
    Unless you want to expand your answer to be more than a link, and to include code snippets or an example, this should be a comment (at best). – Matt Mar 13 '13 at 21:08
  • 1
    I don't know how to add comments haha. Shame on me. – Romain Braun Mar 13 '13 at 21:11
  • Yes, sorry...I'm real savvy with HTML/CSS PHP...but this other stuff is a little outside my understanding, I pieced together the above, but now I'm stuck! – RevConcept Mar 13 '13 at 21:11
  • I responded before you changed your answer...I tried adding your solution in a couple of places within the code I pasted above, but nothing is happening. Where should I put it?? – RevConcept Mar 13 '13 at 21:55
  • What is the URL you want to remove the # from ? Is it $(this).attr('href').substr(31); ? Then do $(this).attr('href').substr(31).replace('#',''); – Romain Braun Mar 13 '13 at 22:01
  • Adding $(this).attr('href').substr(31).replace('#',''); doesn't remove the hash...?? Here is the dev site if you want to see it [link](http://trademarkvisual.com/dev). Clicking on any of the squares will show you what is happening. The full code (including your solution) is in the footer. – RevConcept Mar 13 '13 at 22:38
  • I'm sorry I think I really don't understand what you're trying to do. Could you edit your question or something ? – Romain Braun Mar 14 '13 at 10:36
  • I don't know how to rephrase the question...all I want to do is get the # out of the URL. The replace() method isn't doing it. – RevConcept Mar 14 '13 at 22:33