问题描述:浏览器报错I am loading an <iframe> in my HTML page and trying to access the elements within it using Javascript, but when I try to execute my code, I get the following error:

SecurityError: Blocked a frame with origin "http://www.<domain>.com" from accessing a cross-origin frame.

I am using this code for testing, but in vain:
$(document).ready(function() {var iframeWindow = document.getElementById("my-iframe-id").contentWindow;iframeWindow.addEventListener("load", function() {var doc = iframe.contentDocument ||iframe.contentWindow.document;var target = doc.getElementById("my-target-id");target.innerHTML= "Found it!";});
});

问题原因:

Same-origin security policy

You can't access an <iframe> with Javascript, it would be a huge security flaw if you could do it. For the same-origin policy browsers block scripts trying to access a frame with a different origin.

Origin is considered different if at least one of the following parts of the address isn't maintained:

<protocol>://<hostname>:<port>/path/to/page.html
Protocol, hostname and port must be the same of your domain, if you want to access a frame.

Examples

Here's what would happen trying to access the following URLs from

 http://www.example.com/home/index.html

URL                                             RESULT
http://www.example.com/home/other.html       -> Success
http://www.example.com/dir/inner/another.php -> Success
http://www.example.com:80                    -> Success (default port for HTTP)
http://www.example.com:2251                  -> Failure: different port
http://data.example.com/dir/other.html       -> Failure: different hostname
https://www.example.com/home/index.html.html -> Failure: different protocol
ftp://www.example.com:21                     -> Failure: different protocol & port
https://google.com/search?q=james+bond       -> Failure: different hostname & protocol

Workaround

Even though same-origin policy blocks scripts from accessing the content of sites with a different origin, if you own both the pages, you can work around this problem using window.postMessageand its relative message event to send messages between the two pages, like this:

  • In your main page:

var frame = document.getElementById('your-frame-id'); frame.contentWindow.postMessage(/*any variable or object here*/, '*');

In your <iframe> (contained in the main page):

window.addEventListener('message', function(event) {//IMPORTANT: Check the origin of the data!if (~event.origin.indexOf('http://yoursite.com')) {//The data has been sent from your site//The data sent with postMessage is stored in event.data
console.log(event.data); }else{//The data hasn't been sent from your site!//Be careful! Do not use it.return; }
});

This method can be applied in both directions, creating a listener in the main page too, and receiving responses from the frame. The same logic can also be implemented in pop-ups and basically any new window generated by the main page (e.g. using window.open()) as well, without any difference.

Disabling same-origin policy in your browser

There already are some good answers about this topic (I just found them googling), so, for the browsers where this is possible, I'll link the relative answer. However, please remember thatdisabling the same-origin policy (or the CORS) will only affect your browser. Also, running a browser with same-origin security settings disabled grants any website access to cross-origin resources, so it's very unsafe and should be done for development purposes only.

the best current way of interacting between frames/iframes is using window.postMessage, supported by all browsers

转:http://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame

SecurityError: Blocked a frame with origin from accessing a cross-origin frame相关推荐

  1. iframe跨端口报错 Blocked a frame with origin from accessing a cross-origin frame

    前言     在不同的端口号,甚至是不同的ip进行iframe嵌套的时候,在父页面调用子页面的方法的时候,报错 SecurityError: Blocked a frame with origin f ...

  2. Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy: Cross origin requests

    前言 本地调试js时,遇到本错误 Google Chrome 84.0.4147.135 (正式版本) (64 位) (cohort: Stable Installs Only) 修订版本 c42bd ...

  3. 【VSCode】from origin ‘null‘ has been blocked by CORS policy: Cross origin requests are only supported

    Access to script at 'file:///C:/Users/dawulei/Desktop/%E9%A1%B9%E7%9B%AE/%E5%9D%A6%E5%85%8B%E5%A4%A7 ...

  4. ajax跨域请求问题:Access to XMLHttpRequest at……has been blocked by CORS policy: Cross origin requests

    背景:本地代码调试时,加载本地资源正常,jquery的AJAX请求服务端资源报错. Access to XMLHttpRequest at '***** ' from origin 'null' ha ...

  5. origin和matlab的异同,origin和matlab

    哈哈哈 MATLAB 显示正炫余炫图:plot(x,y1,'* r',x,y2,'o b') 定义[..._origin]=floor((size(se)+1)/2); image_dilation= ...

  6. 解决Cross origin requests are only supported for protocol schemes问题

    解决Cross origin requests are only supported for protocol schemes问题 问题描述 本前端小白在学习的时候尝试按照一个网上的blog的代码来跑 ...

  7. 【跨域异常】Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-exten

    一.背景描述 下面是我用ajax异步请求时的写法,请注意url是怎么写的,下面的错误就是由于这个url的写法导致的. // 获取省份列表 $(document).ready(function () { ...

  8. Jetty Cross Origin Filter解决jQuery Ajax跨域访问的方法

    当使用jQuery Ajax post请求时可能会遇到类似这样的错误提示 XMLHttpRequest cannot load http://xxxxxx. Origin http://xxxxxx ...

  9. git push origin与git push -u origin master的区别

    $ git push origin 上面命令表示,将当前分支推送到origin主机的对应分支. 如果当前分支只有一个追踪分支,那么主机名都可以省略. $ git push 如果当前分支与多个主机存在追 ...

  10. 【chrome错误】Cross origin requests are only supported for protocol schemes: http, data,chrome-extension

    使用ajax请求本地文件,chrome会报跨域错误:XMLHttpRequest cannot loadfile:///C:/Users/Li/Desktop/images/alist.json.Cr ...

最新文章

  1. linux扩充消息队列,Linux关于消息队列的使用分享
  2. 【Java并发编程】20、DelayQueue实现订单的定时取消
  3. 【C#文件锁】C#加密解密文件小工具
  4. cJSON库源码分析
  5. C语言之归并排序算法
  6. mysql试题百度云_BATJ大厂600多道技术面试题及答案解析
  7. JQuery框架2.位置属性|筛选方法|事件
  8. P2158,jzoj1709-仪仗队【欧拉函数,数论】
  9. 怎么看服务器上jdk安装位置,查看云服务器jdk安装路径
  10. How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n”
  11. LeetCode(257)——二叉树的所有路径(JavaScript)
  12. [转]用C#编写ActiveX控件
  13. nanopi制作个人服务器,NanoPi K2 服务器系统镜像
  14. 软件能力成熟度模型(CMMI)
  15. 报错 SocketTimeoutException: null
  16. xp系统如何开启ftp服务器,xp系统怎么开ftp服务器
  17. 《Chrome插件英雄榜》第88篇更新!知乎网页助手让网页版知乎更好用
  18. 基于WeX5平台开发微信公众号中的在线问卷调查
  19. Android Studio 3.6 发布啦,快来围观
  20. 华为OD机试 - 无向图染色

热门文章

  1. 【ORACLE】字符串操作 B字符串时A的一部分
  2. (一)网络编程基础之套接字入门
  3. c#如何取得事件注册的方法
  4. Vue生命周期钩子函数
  5. C#入门详解(10)
  6. YOLO V1论文理解
  7. java实现高性能的数据同步
  8. mysql基本常用命令_mysql 的基本操作以及常用命令
  9. 螃蟹保存方法保存时间_螃蟹吃不完怎么保存?学会这4招,不论是海蟹、河蟹通通都不愁...
  10. 关于stm32f407搭载wifi模块_华为智能体脂秤WiFi版体验:家庭标配的智能家居