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

javascript - Strip off everything after #anchor including the #anchor - Stack Overflow

programmeradmin4浏览0评论

Change this...

www.sample/sample.html#commentxxx?stuffhereIdontneed

into this...

www.sample/sample.html

I have it in a link so I think I need a regex?

document.write("<a href='"+ window.location.href.replace('?????', '') +  
"?ThisIsAppendedLater'>sample</a>");  

Change this...

www.sample.com/sample.html#commentxxx?stuffhereIdontneed

into this...

www.sample.com/sample.html

I have it in a link so I think I need a regex?

document.write("<a href='"+ window.location.href.replace('?????', '') +  
"?ThisIsAppendedLater'>sample</a>");  
Share Improve this question edited Dec 7, 2010 at 8:47 gravityboy asked Dec 7, 2010 at 8:40 gravityboygravityboy 8175 gold badges11 silver badges21 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 17

Nathan's answer is good. For completeness' sake; here's the regex:

var stripped = window.location.href.replace(/#.*$/,'');

window.location.hash = '';

url.substring(0,url.indexOf("#"))

If you want a regex based solution, you can use:

window.location.href.replace('#.*', '');
发布评论

评论列表(0)

  1. 暂无评论