降域

作者: 取个名字都不行 | 来源:发表于2018-03-20 00:00 被阅读0次

1.什么是降域

利用document.domain进行降域
例如http://a.justfun.mehttp://b.justfun.me,后面的justfun.me相同,那么就可以使用document.domain将二者的域名设置为justfun.me,来实现同源

2.降域注意事项

  • 顶级域名无法降域
  • 不仅当前页面要使用document.domain,iframe的源页面也要使用document.domain
  • 降域主要针对于当前页面下的iframe

代码实例

主页面代码

<!doctype html>
<html lang="ch">
<head>
    <meta charset="utf-8">
    <title>使用降域</title>
    <style>
        .container {
            display: flex;
        }

        .container .main, iframe {
            border: 1px solid;
            height: 300px;
            width: 30%;
        }

        .main input {
            width: 300px;
        }
    </style>
</head>
<body>
<h1>降域</h1>
<div class="container">
    <div class="main">
        <input type="text">
    </div>
    <iframe src="http://b.justfun.me:8080/iframe.html" frameborder="0"></iframe>
</div>
<script>
    var inputNode = document.querySelector('.container .main input')
    inputNode.setAttribute('placeholder', '我是当前网页,我的域名是' + location.origin)

    // console.log(window.frames[0].document.body) // 执行这行代码会报错,因为当前网页获取不到任何有关iframe的信息

    document.domain = 'justfun.me'
    // 注意:顶级域名无法进行降域

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

iframe页面代码

<!doctype html>
<html lang="ch">
<head>
    <meta charset="utf-8">
    <title>使用降域</title>
    <style>
        .main input {
            width: 300px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="main">
        <input type="text">
    </div>
</div>
<script>
    var inputNode = document.querySelector('.container .main input')
    inputNode.setAttribute('placeholder', '我是iframe,我的域名是' + location.origin)

    document.domain = 'justfun.me'

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

相关文章

  • 降域

    1.什么是降域 利用document.domain进行降域例如http://a.justfun.me和http:/...

  • 降域 & postMessage

    同源策略 & 跨域[https://www.jianshu.com/writer#/notebooks/48893...

  • Web28.JSONP & 跨域

    跨域 1.JSONP2.CORS3.降域4.PostMessage 同源策略(Same origin Policy...

  • 跨域

    主要内容: 同源策略、跨域实现形式(JSONP、CORS、降域、postMessage) 同源策略 (Same o...

  • 跨域之三:降域 和 postMessage

    本节内容:实现跨域常用的两种方式 —— 降域 和 postMessage 零:跨域报错展示 在非同源情况下,操作 ...

  • 跨域【详解】

    本篇有四种方法跨域:CORS、JSONP、降域、window.postMessage() 1. CORS CORS...

  • 几种跨域的方式

    1. JSONP 2.CORS 3.降域 a.html b.html 4.postMessage跨域 a.htm...

  • 【跨域】JSONP/CORS/降域/postMessage

    浏览器的同源策略 浏览器出于安全方面的考虑,只允许与本域下的接口交互。不同源的客户端脚本在没有明确授权的情况下,不...

  • 使用vue-cli在开发环境中实现跨域步骤

    一、前言 前端常见解决跨域的方法有1、降域、2、jsonp(只支持get方法)、3、CORS(Cross-orig...

  • 跨域-JSONP-CORS-降域-postmessage

    什么是同源策略 浏览器出于安全方面的考虑,只允许与本域下的接口交互。不同源的客户端脚本在没有明确授权的情况下,不能...

网友评论

      本文标题:降域

      本文链接:https://www.haomeiwen.com/subject/yubmqftx.html