js 中location 的学习
生活随笔
收集整理的這篇文章主要介紹了
js 中location 的学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
alert(location.hash);// 空
alert(location.host);//返回服務器名稱和端口號
alert(location.hostname);//返回不帶端口號的服務器名稱
alert(location.href);//返回當前加載頁面的完整的URL
alert(location.toString());//返回當前加載頁面的完整的URL
alert(location.pathname);//返回URL的目錄和文件名
alert(location.port);//返回URL中指定的端口號。如果URL中不包含端口號,則這個屬性返回的空字符串
alert(location.protocol);//返回的協議名稱是http 還是https;
alert(location.search);//返回的是查詢字符串 查詢字符串參數 function getQueryStringArgs(){//取得查詢字符串并去掉開頭的問號var qs=(location.search.length > 0 ? location.search.substring(1) : "");//console.log(qs);//var str='hello world';//var subStr=str.substring(1); //substring 提取從 indexStart 到 indexEnd(不包括)之間的字符,substring(indexStart,indexEnd);// console.log(subStr) //ello world//var subStr=str.substring(1,0)//console.log(subStr) //h//保存數據的對象//var strs=str.split(" ");//console.log(strs);//["hello", "world"]var args={},//取得每一項items=qs.length ? qs.split("&") : [], // split() 方法通過把字符串分割成子字符串來把一個 String 對象分割成一個字符串數組。item=null,name=null,value=null,i=0;len=items.legth;for(i=0;i<len;i++){item=items[i].split("=");name=decodeURIComponent(item[0]);value=decodeURIComponent(item[1]);if(name.length){args[name]=value;}}return args;
} 假設 查詢的字符串是?q=javascript&num=11 var args=getQueryStringArgs();
alert(args["q"]);// javascript
alert(args["num"]);//11 這兩個屬性實則調用location.assign(URL)方法 window.location="http://www.baidu.com"; //跳轉到百度頁面
location.href="http://www.baidu.com";//跳轉到百度頁面 setTimeout(function(){location.replace('http://www.baidu.com');//跳轉到百度后,前進和后退的按鈕被禁用了location.href="http://www.baidu.com";// 跳轉到百度頁面,后退的按鈕沒有被禁用window.location="http://www.baidu.com"; //跳轉到百度頁面,后退的按鈕沒有被禁用location.reload();//重新加載 (有可能從緩沖中加載)location.reload(true);//重新加載 (從服務器重新加載);
},1000); //檢查插件 (在IE中無效) 傳入一個插件名
function hasPlugin(name){name=name.toLowerCase(); // 全轉為小寫for(var i=0;i<navigator.plugins.length;i++){if(navigator.plugins[i].name.toLowerCase().indexOf(name)>-1){return true;}}return false;
}
console.log(hasPlugin("Flash"));//true
//檢測IE中的插件 傳入一個插件名
function hasIEPlugin(name){try{new ActiveXObject(name);return true;} catch(ex){// console.log(ex);return false;}
}
//檢測IE中是否有Flash
console.log(hasIEPlugin("Flash")); //falsefunction hasFlash(){var result=hasPlugin("Flash");if(!result){result=hasIEPlugin("ShockwaveFlash.ShockwaveFlash");console.log(result);}return result;//true
}
console.log(hasFlash());//true
//console.log(hasPlugin('Flash')); //true//console.log(navigator.plugins); //檢測到4個插件
/*for(var i=0;i<navigator.plugins.length;i++){console.log(navigator.plugins[i].name); //Chrome PDF Viewer Shockwave Flash Native Client Chrome PDF Viewer
}
*/
//var str="hello world";
//console.log(str.toLowerCase());//hello world 字符串toLowerCase() 轉換小寫
//console.log(str.toUpperCase()); //HELLO WORLD 字符串toUpperCase() 轉換大寫
//location.href="http://www.baidu.com";
//history.go(-1);
//history.go(1);
//history.go("httt://www.baidu.com");
//history.back();//后退
//0history.forward();//前進
?
//跨瀏覽器獲取視口大小 function getInner() {if (typeof window.innerWidth != 'undefined') {return {width : window.innerWidth,height : window.innerHeight}} else {return {width : document.documentElement.clientWidth,height : document.documentElement.clientHeight}} }//跨瀏覽器獲取滾動條位置 function getScroll() {return {top : document.documentElement.scrollTop || document.body.scrollTop,left : document.documentElement.scrollLeft || document.body.scrollLeft} }//跨瀏覽器獲取innerText function getInnerText(element) {return (typeof element.textContent == 'string') ? element.textContent : element.innerText; }//跨瀏覽器設置innerText function setInnerText(elememt, text) {if (typeof elememt.textContent == 'string') {elememt.textContent = text;} else {elememt.innerText = text;} } //console.log(getInner()); //console.log(getScroll()); //var div=document.getElementsByTagName("div")[0]; //console.log(getInnerText(div)); //setInnerText(div,"abc");?
轉載于:https://www.cnblogs.com/jifengdehao/p/5684829.html
總結
以上是生活随笔為你收集整理的js 中location 的学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: k12教育A股上市公司 投资者最近要
- 下一篇: 2016.07.17-18 集合方法