Home > Web Front-end > JS Tutorial > What is the Window object in JavaScript? What is a Navigator object?

What is the Window object in JavaScript? What is a Navigator object?

青灯夜游
Release: 2018-11-13 11:27:26
forward
3242 people have browsed it

The content of this article is to introduce what is the Window object and what is the Navigator object in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. [Recommended related video tutorials: JavaScript Tutorial]

1. Window Object

1 , Properties of the Window object

(1) closed: Returns whether the window has been closed.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    var myTestWindowdow;
    function openTestWindow(){
        myTestWindowdow=window.open("","","width=200,height=100");
    }
    function closeTestWindow(){
        if (myTestWindowdow){
            myTestWindowdow.close();
        }
    }
    function checkTestWindow(){
        if (!myTestWindowdow){
            document.getElementById("msg").innerHTML="测试窗口没有被打开!";
        }
        else{
            if (myTestWindowdow.closed){
                document.getElementById("msg").innerHTML="测试窗口被关闭!";
            }
            else{
                document.getElementById("msg").innerHTML="测试窗口没有被关闭!";
            }
        }    
    }
</script>
</head>
<body>
    <input type="button" value="打开测试窗口" onclick="openTestWindow()" /><br/>
    <input type="button" value="关闭测试窗口" onclick="closeTestWindow()" /><br/>
    <input type="button" value="测试窗口状态" onclick="checkTestWindow()" /><br/>
    <div id="msg"></div>
</body>
</html>
Copy after login

(2) defaultStatus: Set or return the default text in the window status bar.

 status: Set or return the text of the window status bar.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    //设置默认状态栏文本
    window.defaultStatus="我会显示在浏览器的状态栏中! !";
    //设置状态栏文本
    //window.status="我会显示在浏览器的状态栏中! !";
</script>
</head>
<body>
</body>
</html>
Copy after login

(3) frames: Returns all named frames in the window. The collection is an array of Window objects, each of which contains a frame within the window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function changeAllframe() {
    var frames = window.frames;
    var i;

    for (i = 0; i < frames.length; i++) {
        frames[i].location = "https://www.baidu.com";
    }
}
</script>
</head>
<body>
    <button onclick="changeAllframe()">点我</button><br/>
    <iframe src="https://www.baidu.com"></iframe>
    <iframe src="https://www.taobao.com"></iframe>
    <iframe src="https://www.hao123.com/"></iframe>
</body>
</html>
Copy after login

(4) innerHeight: Returns the height of the document display area of ​​the window.

innerWidth: Returns the width of the document display area of ​​the window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function getWidthAndHeight(){
        var w=window.innerWidth;
        var h=window.innerHeight;
        x=document.getElementById("myInfo");
        x.innerHTML="Width: " + w + " Heigth: " + h;
    }
</script>
</head>
<body>
    <button onclick="getWidthAndHeight()">获取innerWidth,innerHeight</button><br/>
    <div id="myInfo"></div>
</body>
</html>
Copy after login

(5) localStorage: Used to save data for a long time. The saved data has no expiration time until it is manually deleted.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function getClickCount() {
        if(typeof(Storage) !== "undefined") {
            if (localStorage.clickcount) {
                localStorage.clickcount = Number(localStorage.clickcount)+1;
            } else {
                localStorage.clickcount = 1;
            }
            document.getElementById("myInfo").innerHTML = "你已经点击了 " + localStorage.clickcount + " 次。";
        } else {
            document.getElementById("myInfo").innerHTML = "Sorry, your browser does not support web storage...";
        }
    }
</script>
</head>
<body>
    <button onclick="getClickCount()">点击</button><br/>
    <div id="myInfo"></div>
</body>
</html>
Copy after login

When the browser does not support it:

When the browser supports it:

(6 ) length: Returns the number of frames in the current window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function getframeCount() {
        document.getElementById("myInfo").innerHTML = "有frame " + frames.length + " 个。";
    }
</script>
</head>
<body>
    <button onclick="getframeCount()">点击</button><br/>
    <div id="myInfo"></div>
    <iframe src="https://www.baidu.com"></iframe>
    <iframe src="https://www.taobao.com"></iframe>
    <iframe src="https://www.hao123.com/"></iframe>
</body>
</html>
Copy after login

(7) name: Set or return the name of the window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
var myWindow;
    function openWin(){
        myWindow=window.open('','myTestWindow','width=200,height=100');
        myWindow.document.write("<p>窗口名称为: " + myWindow.name + "</p>");
    }
</script>
</head>
<body>
    <button onclick="openWin()">点击</button><br/>
</body>
</html>
Copy after login

(8) opener: Returns a reference to the Window object that created the window. When opening a window using window.open(), you can use this property to return details from the source (parent) window of the target window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function openNewWindow(){
        myNewWindow=window.open('','','width=200,height=100');
        myNewWindow.document.write("这是我新打开的窗口");
        myNewWindow.focus();
        myNewWindow.opener.document.write("这个是源窗口");
    }
</script>
</head>
<body>
    <button onclick="openNewWindow()">点击</button><br/>
</body>
</html>
Copy after login

(9) outerHeight: Returns the outer height of the window, including toolbars and scroll bars.

outerWidth: Returns the outer width of the window, including toolbars and scroll bars.

pageXOffset: Set or return the X position of the current page relative to the upper left corner of the window display area.

pageYOffset: Set or return the Y position of the current page relative to the upper left corner of the window display area.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function getWidthAndHeight(){
        var ow=window.outerWidth;
        var oh=window.outerHeight;
        var pX=window.pageXOffset;
        var pY=window.pageYOffset;
        x=document.getElementById("myInfo");
        x.innerHTML+="outerWidth: " + ow + " outerHeigth: " + oh+"<br/>";
        x.innerHTML+="pageXOffset: " + pX + " pageYOffset: " + pY+"<br/>";
    }
</script>
</head>
<body>
    <button onclick="getWidthAndHeight()">获取</button><br/>
    <div id="myInfo"></div>
</body>
</html>
Copy after login

(10) parent: Return to the parent window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function getParentWindow(){
        window.open('','','width=200,height=100');
        alert(window.parent.location);
    }
</script>
</head>
<body>
    <button onclick="getParentWindow()">获取</button><br/>
    <div id="myInfo"></div>
</body>
</html>
Copy after login

(11) screenLeft: Returns the x coordinate relative to the screen window.

screenTop: Returns the y coordinate relative to the screen window.

screnX: Returns the x coordinate relative to the screen window.

screenY: Returns the y coordinate relative to the screen window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function openNewWindow(){
        myNewWindow=window.open('','');
        myNewWindow.document.write(" 这是新窗口<br/>");
        myNewWindow.document.write(" ScreenLeft: " + myNewWindow.screenLeft +"<br/>");
        myNewWindow.document.write(" ScreenTop: " + myNewWindow.screenTop + "<br/>");
        myNewWindow.document.write(" ScreenX: " + myNewWindow.screenX + "<br/>");
        myNewWindow.document.write(" ScreenY: " + myNewWindow.screenY + "<br/>");
    }
</script>
</head>
<body>
    <button onclick="openNewWindow()">获取</button><br/>
</body>
</html>
Copy after login

(12) sessionStorage: Used to temporarily save the data of the same window (or tab). After closing the window or tab, it will Delete this data.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function getClickCount() {
        if(typeof(Storage) !== "undefined") {
            if (sessionStorage.clickcount) {
                sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
            } else {
                sessionStorage.clickcount = 1;
            }
            document.getElementById("myInfo").innerHTML = "你已经点击了 " + sessionStorage.clickcount + " 次。";
        } else {
            document.getElementById("myInfo").innerHTML = "Sorry, your browser does not support web storage...";
        }
    }
</script>
</head>
<body>
    <button onclick="getClickCount()">点击</button><br/>
    <div id="myInfo"></div>
</body>
</html>
Copy after login

When the browser does not support it:

When the browser supports it:

(13 ) self: Returns a reference to the current window object. Using this attribute, you can ensure that when multiple windows are opened, the functions or properties in the current window are correctly called without confusion.

  top: Returns the topmost browser window of the current window.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function check(){
        if (window.top!=window.self) {
            document.write("这个窗口不是最顶层窗口!")
        }
        else{ 
            document.write("这个窗口是最顶层窗口!</p>")
        } 
    }
</script>
</head>
<body>
    <button onclick="check()">点击</button><br/>
</body>
</html>
Copy after login

2 、Window 对象的方法

(1) alert(): 显示带有一段消息和一个确认按钮的警告框。

   atob(): 解码一个 base-64 编码的字符串。

   btoa(): 创建一个 base-64 编码的字符串。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function myFunction(){
        alert("你好,我是一个警告框!"); //alert方法
        var strA = "Hello world!";
        var btoastr = window.btoa(strA); //btoa() 方法用于创建一个 base-64 编码的字符串。
        var atobstr = window.atob(btoastr); //atob() 方法用于解码使用 base-64 编码的字符串。
        alert("编码字符串为: " + btoastr + "  " + "解码后字符串为: " + atobstr);
    }
</script>
</head>
<body>
    <button onclick="myFunction()">点击</button><br/>
</body>
</html>
Copy after login

  

(2) blur(): 把键盘焦点从顶层窗口移开。focus(): 把键盘焦点给予一个窗口。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function openWin(){
        myWindow=window.open('','','width=200,height=100');
        myWindow.document.write("The new window");
        myWindow.blur();
        //myWindow.focus();
    }
</script>
</head>
<body>
    <input type="button" value="Open window" onclick="openWin()">
</body>
</html>
Copy after login

(3) setInterval(): 按照指定的周期(以毫秒计)来调用函数或计算表达式。

    clearInterval(): 取消由 setInterval() 设置的 timeout。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    //setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
    var myVar = setInterval(function(){ myTimer() }, 1000); 

    function myTimer() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        document.getElementById("timeInfo").innerHTML = t;
    }

    function myStopFunction() {
        clearInterval(myVar); //取消由 setInterval() 函数设定的定时执行操作。
    }
</script>
</head>
<body>
    <button onclick="myStopFunction()">停止时间</button>
    <p id="timeInfo"></p>
</body>
</html>
Copy after login

       

(4) setTimeout(): 在指定的毫秒数后调用函数或计算表达式。

    clearTimeout(): 取消由 setTimeout() 方法设置的 timeout。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    var myVar;

    function myFunction() {
        //setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。
        myVar = setTimeout(function(){ alert("Hello") }, 3000);
    }

    function myStopFunction() {
        clearTimeout(myVar); //如果alert弹出方法还未执行,我们可以使用 clearTimeout() 来阻止它。
    }
</script>
</head>
<body>
    <button onclick="myFunction()">设置3秒弹出Hello</button>
    <button onclick="myStopFunction()">停止弹出</button>
</body>
</html>
Copy after login

(5)  close(): 关闭浏览器窗口。

  open(): 打开一个新的浏览器窗口或查找一个已命名的窗口。

  createPopup(): 创建一个 pop-up 窗口。

  print(): 打印当前窗口的内容。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    var myWindow;
    function openWin(){
        myWindow=window.open('','','width=200,height=100'); //打开新窗口
        myWindow.document.write("使用open()方法打开的新窗口");
    }
    function closeWin(){
        myWindow.close(); //关闭新打开的窗口
    }
    function create_Popup(){
        var p=window.createPopup(); //创建一个 pop-up 窗口
        var pbody=p.document.body;
        pbody.style.backgroundColor="lime";
        pbody.style.border="solid black 1px";
        pbody.innerHTML="这是createPopup()弹出";
        p.show(100,100,200,50,document.body);    
    }
    function my_print(){
        window.print(); //调出windows系统的打印机
    }
</script>

</head>
<body>
    <input type="button" value="open()方法" onclick="openWin()"><br/>
    <input type="button" value="close()方法" onclick="closeWin()"><br/>
    <input type="button" value="createPopup()方法" onclick="create_Popup()"><br/>
    <input type="button" value="print()方法" onclick="my_print()"><br/>
</body>
</html>
Copy after login

 (6) confirm(): 显示带有一段消息以及确认按钮和取消按钮的对话框。

  prompt(): 显示可提示用户输入的对话框。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function my_prompt(){ 
        var x; 
        var Name=prompt("请输入你的名字","Harry Potter"); 
        if (Name!=null && Name!=""){ 
            document.getElementById("myInfo").innerHTML="你好 " + Name + "! ";
        } 
    }
    function my_confirm(){
        var x;
        var r=confirm("请点击");
        if (r==true){
            x="确定";
        }
        else{
            x="取消";
        }
        document.getElementById("myInfo").innerHTML=x;
    }
</script>

</head>
<body>
    <input type="button" value="prompt()方法" onclick="my_prompt()">
    <input type="button" value="confirm()方法" onclick="my_confirm()"><br/>
    <div id=&#39;myInfo&#39;></div>
</body>
</html>
Copy after login

  

  

(7) getComputedStyle(): 获取指定元素的 CSS 样式。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function my_getComputedStyle(){
        var elem = document.getElementById("myInfo");
        var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("background-color");
        document.getElementById("myInfo").innerHTML = theCSSprop;
    }
</script>
</head>
<body>
    <input type="button" value="getComputedStyle()方法" onclick="my_getComputedStyle()">
    <div id=&#39;myInfo&#39; style="height: 50px;background-color: yellow;"></div>
</body>
</html>
Copy after login

(8) moveBy(): 可相对窗口的当前坐标把它移动指定的像素。

   moveTo(): 把窗口的左上角移动到一个指定的坐标。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    function openWin(){
        myWindow=window.open('','','width=200,height=100');
        myWindow.document.write("这是新窗口");
    }
    function moveByWin(){
        myWindow.moveBy(200,200); //相对窗口的当前坐标把它移动指定的像素
        myWindow.focus();
    }
    function moveToWin(){
        myWindow.moveTo(0,0);//把窗口的左上角移动到一个指定的坐标
        myWindow.focus();
    }
</script>
</head>
<body>
    <input type="button" value="打开新窗口" onclick="openWin()" />
    <input type="button" value="moveBy()方法" onclick="moveByWin()" />
    <input type="button" value="moveBy()方法" onclick="moveToWin()" />
</body>
</html>
Copy after login

(9) resizeBy(): 按照指定的像素调整窗口的大小。

  resizeTo(): 把窗口的大小调整到指定的宽度和高度。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    var myWindow;
    function openWin(){
        myWindow=window.open('','','width=200,height=100');
        myWindow.document.write("这是新窗口");
    }
    function resizeByWin(){
        myWindow.resizeBy(100,50);
        myWindow.focus();
    }
    function resizeToWin(){
        myWindow.resizeTo(50,30);
        myWindow.focus();
    }
</script>
</head>
<body>
    <input type="button" value="打开新窗口" onclick="openWin()" />
    <input type="button" value="resizeBy()方法" onclick="resizeByWin()" />
    <input type="button" value="resizeTo()方法" onclick="resizeToWin()" />
</body>
</html>
Copy after login

(10) scrollBy(): 按照指定的像素值来滚动内容。

       scrollTo(): 把内容滚动到指定的坐标。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    var myWindow;
    function openWin(){
        myWindow=window.open('','','width=50,height=20');
        myWindow.document.write("-<br/>");
        myWindow.document.write("---<br/>");
        myWindow.document.write("-----<br/>");
        myWindow.document.write("-------<br/>");
        myWindow.document.write("---------<br/>");
        myWindow.document.write("-----------<br/>");
        myWindow.document.write("-------------<br/>");
        myWindow.focus();
    }
    function scrollByWin(){
        myWindow.scrollBy(20,20); //按照指定的像素值来滚动内容
        myWindow.focus();
    }
    function scrollToWin(){
        myWindow.scrollTo(50,50); // 把内容滚动到指定的坐标
        myWindow.focus();
    }
</script>
</head>
<body>
    <input type="button" value="打开新窗口" onclick="openWin()" />
    <input type="button" value="scrollBy()方法" onclick="scrollByWin()" />
    <input type="button" value="scrollTo()方法" onclick="scrollToWin()" />
</body>
</html>
Copy after login

二、Navigator 对象

1、Navigator 对象的属性

        (1) appCodeName: 返回浏览器的代码名
        (2) appName: 返回浏览器的名称
        (3) appVersion: 返回浏览器的平台和版本信息
        (4) cookieEnabled: 返回指明浏览器中是否启用 cookie 的布尔值
        (5) platform: 返回运行浏览器的操作系统平台
        (6) userAgent: 返回由客户机发送服务器的user-agent 头部的值

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    document.write("浏览器代号: " + navigator.appCodeName);
    document.write("<br/><br/>浏览器名称: " + navigator.appName);
    document.write("<br/><br/>版本信息: " + navigator.appVersion);
    document.write("<br/><br/>是否启用 Cookie: " + navigator.cookieEnabled);
    document.write("<br/><br/>硬件平台: " + navigator.platform);
    document.write("<br/><br/>用户代理: " + navigator.userAgent);
</script>
</head>
<body>
</body>
</html>
Copy after login

2、 Navigator 对象的方法

        (1) javaEnabled(): 指定是否在浏览器中启用Java
        (2) taintEnabled(): 规定浏览器是否启用数据污点(data tainting)

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
    document.write("<br/><br/>启用Java: " + navigator.javaEnabled());
    document.write("<br/><br/>启用数据污点: " + navigator.taintEnabled());
</script>
</head>
<body>
</body>
</html>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of What is the Window object in JavaScript? What is a Navigator object?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template