search
HomeWeb Front-endJS TutorialAbout how to use the properties and methods of window in JS

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
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

See all articles

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor