標籤:
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中不包含連接埠號碼,則這個屬性返回的Null 字元串
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"]);//10
//window.location.href="http:www/baidu.com"; //http://localhost:63342/js%E5%AD%A6%E4%B9%A0/www/baidu.com
//這兩個屬性實則調用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")); //false
function 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");
js 中location 的學習