Detailed window.name attribute (Javascript)

About the attribute of the name under the window

I don’t know if you have discovered such a situation

  • Directly output undeclared variables in the console. Normally, an error should be reported, and the output of declared unassigned variables should be undefined
var a;
//undefined
b;
//报错
  • But there are only a few special cases, the name attribute
    Insert picture description here
  • In fact, the window itself has the attribute name, which can be seen by entering window in the console
    Insert picture description here
  • Open it and turn it down to find

Insert picture description here
The literal translation of window.name is the name of the window, which is mainly used to set targets for hyperlinks and forms. What does it mean? Let’s make a case.

  • Create two pages
  • First page
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>JSdemo1</title>
</head>
<body>
    <a href="./demo2.html" target="hello world">跳转</a>
</body>
</html> 
  • Second page
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>JSdemo2</title>
    <script>
        document.write( window.name + "<br>" + name )
    </script>
</head>
<body>
</body>
</html>
  • Let's open a webpage and click to jump to see the effect
    Insert picture description here
    Insert picture description here

  • Here we can see that the a tag of the first webpage assigns a value to the name attribute of the second window through the target attribute, so that the name attribute of the second webpage has a value.

  • Another thing to note is that if you assign a value to name, window.name will call a method similar to toString to convert the value assigned to it into the corresponding string representation.
    Insert picture description here

Now that we know the properties of window.name, we can try the following question.

var name = 123;
var obj = {
    
    };
console.log(name + 123 + obj )

Welcome everyone to answer in the comment area! ! !

Guess you like

Origin blog.csdn.net/qq572069832/article/details/109751956