会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 128778个问题
WEB前端全系列/第二阶段:JavaScript编程模块/编程风格 571楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 573楼

<!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编程模块/面向对象编程 574楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 575楼
WEB前端全系列/第二阶段:JavaScript编程模块/函数与对象 576楼
WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 581楼

OCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{padding: 0;margin: 0}
        ul{
            list-style: none;background-color: azure;width: 220px;
            display: none;position: absolute;}
        li{
            height: 30px;line-height: 30px;padding: 5px 10px;
        }
        li:hover{background-color: orange}
    </style>
</head>
<body>
<ul>
    <li>真的要离开么</li>
    <li>去尚学堂</li>
    <li>搜索选中的内容</li>
    <li>搜索输入的内容</li>
</ul>
<textarea  cols="30" rows="10"></textarea>
<script>
    window.oncontextmenu=function () {
  return false
    }
    var  ul=document.querySelector('ul');
    document.onmouseup=function (eve) {
    if (eve.button==2){
        ul.style.display='block';
        ul.style.top=eve.clientY+'px';
        ul.style.left=eve.clientX+'px';
    }else {ul.style.display='none'}
    }
    ul.onclick=function (eve) {
    if (eve.target.innerHTML=='去尚学堂'){
        alert('那就去吧')
    }
    else if (eve.target.innerHTML=='真的要离开么'){
        if (confirm('真的要离开么')){window.close()}
    }else if (eve.target.innerHTML=='搜索选中的内容'){
      var ret=document.getSelection().toString();
        window.open('http://www.baidu.com/s?wd='+ret)
        // console.log(ret)

    }else {
        var ret=prompt('搜索输入的内容');
        window.open('http://www.baidu.com/s?wd='+ret)

    }
    }
</script>
</body>
</html>
  1. window.close()无法关闭当前窗口

  2. var ret=document.getSelection().toString();
    返回的不是字符串,只是跳转到百度首页,无法跳转到搜索的内容

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 583楼

百战程序员微信公众号

百战程序员微信小程序

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