search
HomeWeb Front-endFront-end Q&AWhat does window mean in javascript

What does window mean in javascript

Feb 15, 2022 pm 06:06 PM
javascriptwindow

In JavaScript, window means "window" and is a built-in host object that represents the browser window. All browsers support this object. All JavaScript global objects, functions, and variables will automatically become members of the window object.

What does window mean in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, window means "window" and is a built-in host object.

The window object is the core of all objects in the BOM. In addition to being the parent object of all objects in the BOM, it also contains some window control functions.

The host object is the object provided by the environment that executes the JS script, and is the object provided by the browser. All BOM and DOM are host objects.

Window object

All browsers support the window object. It represents the browser window.

All JavaScript global objects, functions, and variables automatically become members of the window object.

  • Global variables are properties of the window object.

  • Global functions are methods of the window object.

Even the document of HTML DOM is also one of the attributes of the window object:

window.document.getElementById("header");

Same as this:

document.getElementById("header");

1. Global window object

Any global function or variable in JavaScript is a property of window.

<script>
    var name="撼地神牛";
    document.write(window.name);
</script>

2. Window and self objects

The self object is exactly the same as the window object. Self is usually used to confirm that it is in the current form.

<script>
    document.write(window == self);      //必须相等,永远都相等
  document.write(window.Top == window.self);  //判断当前框架是否是主框架
</script>

Window, self, window.self are equivalent.

3. Sub-objects of window

The main objects of window mainly include the following:

  • JavaScript document object

  • JavaScript frames object

  • JavaScript history object

  • JavaScript location object

  • JavaScript navigator object

  • JavaScript screen object

4. Window function index (only valid for IE)

Form control Function:

  • JavaScript moveBy() Function: Move the form x pixels horizontally and y pixels vertically from the current position. If x is a negative number, it will move the form to the left. A negative y will move the form upward.

  • JavaScript moveTo() function: Move the upper left corner of the form to the (x, y) point relative to the upper left corner of the screen. When a negative number is used as a parameter, the form will be moved out of the screen. visible area.

  • JavaScript resizeBy() function: Relative to the current size of the form, adjust the width by w pixels and the height by h pixels. If the parameter is negative, the form will be reduced, otherwise it will be enlarged.

  • JavaScript resizeTo() function: Adjust the width of the form to w pixels and the height to h pixels.

    <input>
    <input>
    <input>
    <input>
    <input>
    <input>
    <input>

Form scroll axis control function:

  • JavaScript scrollTo() function: If there is a scroll bar in the form, it will scroll horizontally The bar is moved to a position x pixels relative to the width of the form, and the vertical scroll bar is moved to a position y pixels relative to the height of the form.

  • JavaScript scrollBy() function: If there is a scroll bar, move the horizontal scroll bar to a position of x pixels relative to the current horizontal scroll bar (that is, move x pixels to the left), Move the vertical scroll bar to a position that is y pixels relative to the height of the current vertical scroll bar (that is, move y pixels downward).

Note the difference, one is relative to the current window, and the other is relative to the current position of the scroll bar.

<div style="height:150%; width:150%; background-color:#ddd">
    <input type="button" id="btn1" value="移动滚动条!" onclick="window.scrollTo(100,100);" />  //相当于设置绝对位置
    <input type="button" id="btn1" value="移动滚动条!" onclick="window.scrollBy(100,100);" />  //相当于累加
</div>

Form focus control function:

  • JavaScript focus() function: Make the form or space gain focus

  • JavaScript blur() function: Make the form or control lose focus

<div>
    <input type="button" value="获得焦点" onclick="document.getElementById(&#39;testInput&#39;).focus()" />
    <input type="button" value="失去焦点" onclick="document.getElementById(&#39;testInput&#39;).blur()" />
    <input type="text" value="text" id="testInput" onblur="alert(&#39;我已失去焦点&#39;)" />
</div>

New form function:

  • JavaScript open() function: Open (pop-up) A new form
  • JavaScript close() function: close the form
  • JavaScript opener attribute: Communication between cross-forms can be achieved through opener, but it must be under the same domain name , and one form should contain the opener of another form.

window.open(url, name, features, replace);

Open function parameter description:

  • url -- to load the form URL;
  • name -- the name of the new form (can also be the value of the HTML target attribute, target);
  • features -- a string representing the form's features, a string Each attribute in is separated by commas;
  • replace -- a Boolean value indicating whether the newly loaded page replaces the currently loaded page. This parameter is usually not specified.

Open method example:

  <a href="2.html" target="2">在新窗口打开连接</a>
  <a href="#" onclick="window.open(&#39;http://www.google.com&#39;,&#39;2&#39;);">在已建立连接的页面打开新地址</a>

First use a normal HTML link to open a page (target named dreamdu), and then use the open function to open another page. The browser first needs to find whether There is a form named dreamdu. If so, load the open address in this form.

Set open

window.open (&#39;page.html&#39;, &#39;newwindow&#39;, &#39;height=100, width=400, top=0,left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no&#39;)

Pop-up window method:

方法一:<body onload="openwin()"> 浏览器读页面时弹出窗口;  
方法二:<body onunload="openwin()"> 浏览器离开页面时弹出窗口;

Description of the third parameter features of the open function:

## resizableBooleanWhether the form allows resizing by dragging the edges, the default value is no##scrollabletoolbartopstatuswidth

  特性字符串中的每个特性使用逗号分隔,每个特性之间不允许有空格。

  window.open函数新建立窗体后会返回新建窗体的window对象,通过此对象可以控制窗体(移动,改变大小,关闭)。

  close函数:

<input type="button" value="关闭已经打开的窗体!" onclick="window.close();" />

  self.close();配合上setTimeout()可以实现,打开的窗口定时关闭的效果。

  对话框函数:

  • JavaScript alert() 函数:弹出消息对话框(对话框中有一个OK按钮)

  • JavaScript confirm() 函数:弹出消息对话框(对话框中包含一个OK按钮与Cancel按钮)

  • JavaScript prompt() 函数:弹出消息对话框(对话框中包含一个OK按钮、Cancel按钮与一个文本输入框)

  alert();

  不说。

  confirm(str);

  confirm()消息对话框是排它的,也就是在用户点击对话框的按钮前,不能进行任何其它操作。

if(confirm("确定跳大?"))
{
    alert("果断跳大");
}else{
    alert("猥琐打钱");
}

  显示如下:

    

  prompt(str1, str2);

  函数有两个参数

  • str1 -- 要显示在消息对话框中的文本,不可修改
  • str2 -- 文本框中的内容,可以修改
var sResult=prompt("请在下面输入你的姓名", "撼地神牛");if(sResult!=null)
{
    alert(sResult + "已经超越神的杀戮");
}else{
    alert("无名氏已经超越神的杀戮");
}

  显示如下:

  

  时间等待与间隔函数:

  • JavaScript setTimeout() 函数
  • JavaScript clearTimeout() 函数
  • JavaScript setInterval() 函数
  • JavaScript clearInterval() 函数

  1、setTimeout()、clearTimeout()  在指定的时间后调用函数

  语法:

  • setTimeout(fun,time);  fun:函数体或函数名,time指定时间,单位为毫秒。
  • clearTimeout(id);  取消指定的setTimeout函数将要执行的代码
    setTimeout(function () {
        document.write("隔3秒后触发");   
    }, 3000)    //在3秒后输出
    setTimeout(fun1, 5000);     //在5秒后输出
    function fun1() {
        document.write("函数名的方式5秒后触发");
    }

  2、setInterval()、clearInterval(value)  在间隔指定的事件后重复调用函数

  语法:

  • setInterval(fun1,time)  fun:函数体或函数名,time指定的时间,单位为毫秒。会返回一个值,这个值是统计该函数的个数用的,第一个是1,第二个就是2,指明是第几个setInterval函数。
  • clearInterval(value)    value:setInterval()函数返回的值,根据这个值可以停止setInterval()的重复。 
var i = 0;var h = setInterval(function () {
    document.write("3秒输出一次<br/>");
    i++;    if (i >= 3) {
       clearInterval(h);
        document.write("停止输出");
    }
}, 3000);

  注意,javascript是单线程的,因此,这个定时函数实际上是通过插入执行队列的方式来实现。

  如下面的代码:

function fn() { 
  setTimeout(function(){alert(&#39;can you see me?&#39;);},1000); 
  while(true) {} 
}

  alert();永远都不会执行,因为线程一直被死循环占用了。

window.location子对象 

  解析URL对象location

  location对象的属性有:href,protocal,host,hostname,port,pathname,search,hash

        document.write(location.href + "<br/>");        // http://localhost:4889/javascriptTest.html
        document.write(location.protocol + "<br/>");    // http:
        document.write(location.host + "<br/>");        // localhost:4889
        document.write(location.hostname + "<br/>");    // localhost
        document.write(location.port + "<br/>");        // 4889
        document.write(location.pathname + "<br/>");    // /javascriptTest.html
        document.write(location.search + "换行<br/>");  //http://localhost:4889/javascriptTest.html?id=1&name=张三 如果路径是这样,则输出  ?id=1&name=%E5%BC%A0%E4%B8%89
        document.write(location.hash);                  //http: //localhost:4889/javascriptTest.html#kk=你好?id=1&name=张三 如果路径是这样,则输出  #kk=你好?id=1&name=张三

  载入新文档

    location.reload()  重新加载页面

    location.replace()  本窗口载入新文档

    location.assign()   本窗口载入新文档

    location = "http://www.baidu.com"  //跳转到指定网址

    location = "search.html"        //相对路径跳转

    location = "#top"      //跳转到页面顶部

  浏览历史

    History()对象的back()与forward()  与浏览器的“后退”,"前进"功能一样。

    history.go(-2);  后退两个历史记录

  浏览器和屏幕信息

    navigator.appName  Web浏览器全称

    navigator.appVersion  Web浏览器厂商和版本的详细字符串

    navigator.userAgent  客户端绝大部分信息

    navagator.platform   浏览器运行所在的操作系统

        document.write(navigator.userAgent + "<br/>"); // Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
        document.write(navigator.appName + "<br/>");   //Netscape
        document.write(navigator.appVersion + "<br/>"); //5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
        document.write(navigator.platform);             //Win32

窗口的关系

    parent == self  只有顶级窗口才返回true

     parent和top属性允许脚本引用它的窗体的祖先,通常窗体是通过

5、event事件对象

  最有用的两个操作:阻止事件冒泡。有时return false;不管用,这个或许就管用了。

//IE:
window.event.cancelBubble = true;//停止冒泡
window.event.returnValue = false;//阻止事件的默认行为

//Firefox:
event.preventDefault();// 取消事件的默认行为  
event.stopPropagation(); // 阻止事件的传播

【相关推荐:javascript学习教程

Parameter name Type Description
height Number Set the height of the form, which cannot be less than 100
left Number Describe the left coordinate of the created form, which cannot be negative Value
location Boolean Whether the form displays the address bar, the default value is no
Boolean Whether dragging is allowed when the interior of the form exceeds the visible range of the window. The default value is no
Boolean Whether the form displays the toolbar, the default value is no
Number Describes the upper coordinate of the created form, which cannot be a negative value
Boolean Whether the form displays the status bar, the default value is no
Number The width of the created form cannot be less than 100

The above is the detailed content of What does window mean in javascript. 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
React vs. Backend Frameworks: A ComparisonReact vs. Backend Frameworks: A ComparisonApr 13, 2025 am 12:06 AM

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

HTML and React: The Relationship Between Markup and ComponentsHTML and React: The Relationship Between Markup and ComponentsApr 12, 2025 am 12:03 AM

The relationship between HTML and React is the core of front-end development, and they jointly build the user interface of modern web applications. 1) HTML defines the content structure and semantics, and React builds a dynamic interface through componentization. 2) React components use JSX syntax to embed HTML to achieve intelligent rendering. 3) Component life cycle manages HTML rendering and updates dynamically according to state and attributes. 4) Use components to optimize HTML structure and improve maintainability. 5) Performance optimization includes avoiding unnecessary rendering, using key attributes, and keeping the component single responsibility.

React and the Frontend: Building Interactive ExperiencesReact and the Frontend: Building Interactive ExperiencesApr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React and the Frontend Stack: The Tools and TechnologiesReact and the Frontend Stack: The Tools and TechnologiesApr 10, 2025 am 09:34 AM

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React's Role in HTML: Enhancing User ExperienceReact's Role in HTML: Enhancing User ExperienceApr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React Components: Creating Reusable Elements in HTMLReact Components: Creating Reusable Elements in HTMLApr 08, 2025 pm 05:53 PM

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

React Strict Mode PurposeReact Strict Mode PurposeApr 02, 2025 pm 05:51 PM

React Strict Mode is a development tool that highlights potential issues in React applications by activating additional checks and warnings. It helps identify legacy code, unsafe lifecycles, and side effects, encouraging modern React practices.

React Fragments UsageReact Fragments UsageApr 02, 2025 pm 05:50 PM

React Fragments allow grouping children without extra DOM nodes, enhancing structure, performance, and accessibility. They support keys for efficient list rendering.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools