Home  >  Article  >  Web Front-end  >  About how to use the properties and methods of window in JS

About how to use the properties and methods of window in JS

不言
不言Original
2018-07-16 17:25:072324browse

This article mainly introduces the analysis of window attributes and methods in JS. It has certain reference value. Now I share it with everyone. Friends in need can refer to it.

Objects can be written without using window This prefix

setInterval() – executes the specified code continuously for the specified number of milliseconds.

clearInterval() – Function code used to stop the execution of the setInterval() method.

setTimeout() - Execute the specified code after pausing for the specified number of milliseconds

clearTimeout() - Function code used to stop executing the setTimeout() method

Example:

Use a timer to display the clock on the page
•The time format is: xxxx year xx month xx day xx:xx:xx to seconds
•Refresh once per second

1 <body>
2     <button onclick="open1()">打开新页面</button>
3     <button onclick="start1()">开始显示时间</button>
4     <button onclick="stop1()">停止时间</button>
5 </body>
<script type="text/javascript">
            function open1(){
                window.open("new_file.html","newFile","menubar=no,location=no,toolbar=no,resize=no,width=500,height=500,top=200,left=400")
            }
            
            function time1(){
            var date = new Date();
            var y = date.getFullYear();
            var mo = date.getMonth();
            var d = date.getDate();
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();
            console.log("%d年%d月%d日 %d:%d:%d" ,y , mo , d , h , m , s );                
            }
            var inter = null ;
            
            function start1(){
                if(inter != null){
                    stop1();
                }
                inter = setInterval(time1,1000);
            }
            
            function stop1(){
                clearInterval(inter);
                inter = null;
            }
            
            
        </script>

close( ) - Close the current window

open() - Open a new window and return the object of the new window

Syntax window.open(URL,name,features,replace);

URL: Optional string that declares the URL of the new window. If this parameter is omitted or the value is an empty string, the new window will not display any documents

name: optional string, which is a comma-separated list of features that declares the name of the new window. If this parameter specifies a window that already exists, the open method returns a reference to the specified window (no new window is created). At this time, features will be ignored.

features: Optional string that declares the standard browser features displayed in the new window. If omitted, the new window has all standard features.

replace: an optional boolean value. Specifies whether a URL loaded into a window creates a new entry in the window's browsing history, or replaces the current entry in the browsing history. The following values ​​are supported:•true - The URL replaces the current entry in the browsing history. •false - The URL creates a new entry in the browsing history.

The third parameter of the open() method is as follows

Whether the window is resizable. The default is yes. Whether to display scroll bars. The default is yes. Whether to add a status bar. The default is yes. Whether to display the title bar. The default is yes. Whether to display the browser toolbar. The default is yes. The y coordinate of the window. The width of the document display area of ​​the window. Measured in pixels.
##channelmode=yes|no|1|0

Whether to use theater mode to display the window. Default is no.

resizable=yes|no|1|0

directories=yes|no|1|0

Whether to add a directory button. Default is yes.

scrollbars=yes|no|1|0

fullscreen=yes|no|1|0

Whether to display the browser in full screen mode. The default is no.

A window in full screen mode must also be in theater mode.

status=yes|no|1|0

height=pixels

The height of the window document display area. Measured in pixels.

titlebar=yes|no|1|0

left=pixels

The x-coordinate of the window. Measured in pixels.

toolbar=yes|no|1|0

location=yes|no|1|0

Whether to display the address field. The default is yes.

top=pixels

menubar=yes|no|1|0

Whether to display the menu bar. The default is yes.

width=pixels

Location object properties

hash

Settings Or returns a URL starting with a pound sign (#) (anchor).

host

Set or return the host name and port number of the current URL.

hostname

Sets or returns the hostname of the current URL.

href

Sets or returns the full URL.

pathname

Sets or returns the path portion of the current URL.

port

Set or return the port number of the current URL.

protocol

Set or return the protocol of the current URL.

search

Sets or returns the URL (query part) starting with a question mark (?).

The following is an example

<script type="text/javascript">
var hash = location.hash;// top
var host = location.host;// www.baidu.com:8020
var hostname = location.hostname;// www.baidu.com
var port = location.port;// 8020;
var pathname = location.pathname;// index.html
var protocol = location.protocol; // http
console.log(location);
console.log(hash);
console.log(host);
console.log(hostname);
console.log(port);
console.log(pathname);
console.log(protocol);
</script>

Location object method

assign() Load a new document.

reload() Reload the current document, which is equivalent to refreshing the page.

replace() Replace the current document with a new document.

(location.replace("location.html#top?a=10&b=20"); is equivalent to location.href = "location.html#top?a=10&b=20";)

Related recommendations:

How to transfer functions in js

Parsing of custom objects in js

The above is the detailed content of About how to use the properties and methods of window in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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