最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery how to remove # from url - Stack Overflow

programmeradmin3浏览0评论

My url generates like this: only shows in ie9


how do I remove # from url and make it like on rest of my pages? thanks.

can i use like this? how can I detect # cause front page has no #.

if ($.browser.msie  && parseInt($.browser.version, 10) === 9 && window.location.href.indexOf("#")) 
    {
        document.location.href = String( document.location.href ).replace( /#/, "" );
    }

to remove #/ used .replace( /#\//, "" ); as mentioned Kevin B

My url generates like this: only shows in ie9

http://myurl.com/#/categories/posts

how do I remove # from url and make it like http://myurl.com/categories/posts on rest of my pages? thanks.

can i use like this? how can I detect # cause front page has no #.

if ($.browser.msie  && parseInt($.browser.version, 10) === 9 && window.location.href.indexOf("#")) 
    {
        document.location.href = String( document.location.href ).replace( /#/, "" );
    }

to remove #/ used .replace( /#\//, "" ); as mentioned Kevin B

Share Improve this question edited Apr 5, 2012 at 21:46 test asked Apr 5, 2012 at 21:19 testtest 2,46615 gold badges46 silver badges81 bronze badges 2
  • Are you adding that for purposes of maintaining a history? You're going to need to provide a bit more background on your configuration. – Chords Commented Apr 5, 2012 at 21:20
  • Yes, also where do you want to remove it from? In my answer I assumed the address bar, Jones in his assumed all links in the page, it's not quite clear. – gotofritz Commented Apr 5, 2012 at 21:24
Add a comment  | 

4 Answers 4

Reset to default 9

It's not really a JQuery job - use normal Javascript for this.

document.location.href = String( document.location.href ).replace( /#/, "" );

EDIT Adding @Kevin B's answer for completeness

document.location.href = String( document.location.href ).replace( "#/", "" );

Just add 'return false' for anchor tag's click event.

$("#selectorname").click(function(){
    if ($.browser.msie  && parseInt($.browser.version, 10) === 9 && window.location.href.indexOf("#")) 
    {
        document.location.href = String( document.location.href ).replace( /#/, "" );
    }
    return false; /* This prevents url from # */
});

or else

<a href="#" onclick='return false'>Hey click me, I'm # free</a>

or just make it simple

$("#selectorname").click(function(e){
   /* some statements */
   e.preventDefault();
});

This prevents the default action not to be triggered. Detailed info here

$("a").attr("href",$("a").attr("href").replace(/#/, "")); 

<a href="#" onclick="return false">Hey click me, I'm # free</a> does the work. I used this to hide # during Bootstrap dynamic tab switch. Thanks

<ul class="nav nav-tabs">
                            <li class="active"><a data-toggle="tab" href="#i-ab" onclick="return false">ab</a></li>
                            <li><a data-toggle="tab" href="#i-cd" onclick="return false">cd</a></li>
                            <li><a data-toggle="tab" href="#i-ef" onclick="return false">ef</a></li>
                        </ul>
发布评论

评论列表(0)

  1. 暂无评论