会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>window对象</title>
</head>
<body>
    <button>跨域传输</button>
    <script>
        // open('http://www.baidu.com');
        // close();
        // var but =document.querySelector('button');
        // but.onclick=function(){
        //     var iframe = document.createElement('iframe');
        //     iframe.src='name.html';
        //     iframe.style.display='none';
        //     document.body.appendChild(iframe);
        //     iframe.onload=function(eve){
        //         var iframeWindowName = eve.target.contentWindow.name;
        //         console.log(iframeWindowName);
        //     }
        // }
        var but= document.querySelector('button');
        but.onclick=function(){
       var iframe= document.createElement('iframe');
        iframe.src='name.html';//加载保存了信息的页面
        iframe.style.display='none';
        document.body.appendChild(iframe);
        //当iframe加载完毕,意味着window.name的内容已经被赋予完毕
        iframe.onload=function(eve){
            var iframeWindowName=eve.target.contentWindow.name;
           // console.log( iframeWindowName);
            eval(iframeWindowName);
            console.log(num);
        }
    }

    </script>
</body>
</html>

跟着老师的代码敲的,无奈报错,于是把老师的代码拿过来 仍然报错,如图

image.png

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 588楼
WEB前端全系列/第二阶段:JavaScript编程模块/编程风格 591楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 593楼

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>106.window对象的name属性</title>
</head>
<body>
    <button>跨域传输</button>
<script>
   
    //console.log(num);
    //如果想要获取一个页面内的信息,那么必须先加载
    var but= document.querySelector('button');
    but.onclick=function(){
        var iframe= document.createElement('iframe');
        iframe.src='106中的page.html';//加载保存了信息的页面
        iframe.style.display='none';  //加载过来不显示
        document.body.appendChild(iframe);
        //当iframe加载完毕,意味着window.name的内容已经被赋予完毕
        iframe.onload=function(eve){
            var iframeWindowName=eve.target.contentWindow.name;
            console.log( iframeWindowName);
            console.log(typeof iframeWindowName);
            eval(iframeWindowName);  //解析字符串
            console.log(num);
        }
    }  
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    var num=10;
    window.name='var num=10;';
    window.name='var num=[1,2,3];'; //可以是数组
    window.name='var num={age:22};';//对象也可以
    
</script>
</body>
</html>

微信图片_20200512190140.jpg

老师我按视频中的代码敲的,为啥会出现这种结果,这是什么情况,浏览器的版本不同吗???

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 594楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 595楼
WEB前端全系列/第二阶段:JavaScript编程模块/函数与对象 596楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637