获取当前网址但没有http://部分书签!

Bos*_*jan 11 javascript url host bookmarklet

伙计们我有一个问题,希望你能帮我解决这个问题.我有一个书签;

javascript:q=(document.location.href);void(open('http://other.example.com/search.php?search='+location.href,'_self ','resizable,location,menubar,toolbar,scrollbars,status'));
Run Code Online (Sandbox Code Playgroud)

获取当前网页的URL并在另一个网站中搜索它.当我使用这个书签时,它会包含整个URL http://并搜索它.但是现在我想更改这个书签,所以它只需要www.example.com或只是example.com(不http://)和搜索这个网址.是否可以这样做,你可以帮助我这个吗?

谢谢!

小智 21

JavaScript可以部分访问当前URL.对于此网址:

http://css-tricks.com/example/index.html

window.location.protocol = "http"

window.location.host = "css-tricks.com"

window.location.pathname = "/example/index.html"
Run Code Online (Sandbox Code Playgroud)

请查看:http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/


jit*_*ter 9

这应该做到这一点

location.href.replace(/https?:\/\//i, "")
Run Code Online (Sandbox Code Playgroud)

  • 固定它.处理http/https + caseinsensitve匹配.也只替换第一次出现. (3认同)