jQuery how to remove # from url

2020-02-15 01:30发布

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

4条回答
家丑人穷心不美
2楼-- · 2020-02-15 01:50

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( "#/", "" );
查看更多
相关推荐>>
3楼-- · 2020-02-15 01:51
$("a").attr("href",$("a").attr("href").replace(/#/, "")); 
查看更多
Viruses.
4楼-- · 2020-02-15 01:57

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

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-15 02:09

<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>
查看更多
登录 后发表回答