首先,我們這裏有一個 url
,是 http://www.a.com/list/2.html?page=2&color=4&size=3#pic
.
我們訪問訪問這個地址,打開控制檯,輸入window.location
,會得到如下圖的結果
一、獲取路徑
/設置或獲取對象指定的文件名或路徑。
alert(window.location.pathname);
//設置或獲取整個 URL 爲字符串。
alert(window.location.href);
//設置或獲取與 URL 關聯的端口號碼。
alert(window.location.port);
//設置或獲取 URL 的協議部分。
alert(window.location.protocol);
//設置或獲取 href 屬性中在井號“#”後面的分段。
alert(window.location.hash);
//設置或獲取 location 或 URL 的 hostname 和 port 號碼。
alert(window.location.host);
//設置或獲取 href 屬性中跟在問號後面的部分。
alert(window.location.search);
二、取出url?號後面的參數
總結:用window.location.search可獲得?tel=15811296111&status=1&id=100
<script>
var url = 'http://www.deikang.com/index.php?tel=15811296111&status=1&id=100';
var n = url.indexOf('?');
console.log(n);//32
var m = url.substring(n + 1, url.length);
console.log(m);//tel=15811296111&status=1&id=100
var arr = m.split('&');
console.log(arr);//["tel=15811296111", "status=1", "id=100"]
for (var i in arr) {
//alert(arr[i]);
//var reg=new RegExp('tel=');
//test() 方法用於檢測一個字符串是否匹配某個模式.
var reg = /tel=/;
if (reg.test(arr[i])) {
console.log(arr[i]);//15811296111
}
}
</script>
js實現trim() JS去掉首尾空格 JS去掉兩頭空格
function trimStr(str){
return str.replace(/(^\s*)|(\s*$)/g,"");
}
console.log(trimStr(' https://blog.csdn.net/muzidigbig '));//https://blog.csdn.net/muzidigbig