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"));  //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");

 



posted @ 2016-07-19 14:56  jifengdehao  阅读(1056)  评论(0编辑  收藏  举报