JavaScript基础(8) BOM

来源:互联网 发布:手机时间软件下载 编辑:程序博客网 时间:2024/05/29 07:58

 8.1 window对象

8.1.1 全局作用域   

    <script type="text/javascript">        var age = 29;        window.color = "red";                //throws an error in IE < 9, returns false in all other browsers        delete window.age;        //throws an error in IE < 9, returns true in all other browsers        delete window.color;    //returns true                alert(window.age);      //29        alert(window.color);    //undefined    </script>


8.1.3 窗口位置

    <script type="text/javascript">        var leftPos = (typeof window.screenLeft == "number") ?                           window.screenLeft : window.screenX;        var topPos = (typeof window.screenTop == "number") ?                           window.screenTop : window.screenY;        alert("Left: " + leftPos);        alert("Top: " + topPos);    </script>

8.1.5 导航与打开窗口

    <script type="text/javascript">            var blocked = false;                try {            var wroxWin = window.open("http://www.wrox.com", "_blank");            if (wroxWin == null){                blocked = true;            }        } catch (ex){            blocked = true;        }                if (blocked){            alert("The popup was blocked!");        }    </script>


8.1.6 间歇调用和超时调用


    <script type="text/javascript">        var num = 0;        var max = 100;                function incrementNumber() {            num++;                    //if the max has not been reached, set another timeout            if (num < max) {                setTimeout(incrementNumber, 500);            } else {                alert("Done");            }        }                setTimeout(incrementNumber, 500);    </script>

8.1.7 系统对话框

alert(), confirm(), prompt()

8.2 location对象

8.2.1 查询字符串参数

  <script type="text/javascript">                           function getQueryStringArgs(){                    //get query string without the initial ?            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),                            //object to hold data                args = {},                            //get individual items                items = qs.length ? qs.split("&") : [],                item = null,                name = null,                value = null,                                //used in for loop                i = 0,                len = items.length;                        //assign each item onto the args object            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;        }        //assume query string of ?q=javascript&num=10                var args = getQueryStringArgs();                alert(function(){return location.search;}());        alert(args["q"]);     //"javascript"        alert(args["num"]);   //"10"    </script>

8.2.2 位置操作

location.assign(" URL ")  
window.location =  URL
       location.href = URL
location.replace(URL) //不会生成历史
location.reload()//重新加载
location.reload(true)//重新加载(从服务器)

8.3 navigator对象

8.3.1检测插件

    <script type="text/javascript">
    
        //plugin detection - doesn't work in 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;
        }        
    
        //plugin detection for IE
        function hasIEPlugin(name){
            try {
                new ActiveXObject(name);
                return true;
            } catch (ex){
                return false;
            }
        }
        
        //detect flash for all browsers
        function hasFlash(){
            var result = hasPlugin("Flash");
            if (!result){
                result = hasIEPlugin("ShockwaveFlash.ShockwaveFlash");
            }
            return result;
        }
        
        //detect quicktime for all browsers
        function hasQuickTime(){
            var result = hasPlugin("QuickTime");
            if (!result){
                result = hasIEPlugin("QuickTime.QuickTime");
            }
            return result;
        }
        
        //detect flash
        alert(hasFlash());
        
        //detect quicktime
        alert(hasQuickTime());
    </script>





0 0