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

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

Jul 16, 2018 pm 05:25 PM

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>
Copy after login
<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>
Copy after login

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>
Copy after login
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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles