There are many other parameters than the href
in window.location
. See full reference here: https://developer.mozilla.org/en/DOM/window.location
What you are looking for as a starter might be the window.location.hostname
:
"the host name (without the port number or square
brackets)."
From the example URL http://[www.example.com]:80/search?q=devmo#test
the hostname will be www.example.com
.
If you also want to include the path and force a http:// protocol, try:
'http://' + window.location.hostname + window.location.pathname;
As a side note, a nifty trick to get the same parameters from another URL than the window.location is to create an empty anchor:
var a = document.createElement('a');
a.href = 'http://www.example.com:80/search?q=devmo#test';
console.log('http://' + a.hostname + a.pathname);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…