在 Chrome 中对本地文件使用 iframe

编程入门 行业动态 更新时间:2024-10-25 22:29:51
本文介绍了在 Chrome 中对本地文件使用 iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很难弄清楚如何从外部页面访问 iframe 中加载的页面.两个页面都是本地文件,我使用的是 Chrome.

I am having a tough time figuring out how to access a page loaded in an iframe from the outer page. Both pages are local files, and I'm using Chrome.

我有一个外页和许多内页.外页应始终显示内页的页面标题(这在我的应用程序中很有意义,在这个精简的示例中可能不那么有意义).这在 AppJS 中没有任何问题,但我被要求让这个应用程序直接在浏览器中工作.我收到错误阻止了一个源为null"的帧访问源为null"的帧.协议、域和端口必须匹配.".

I have an outer page, and many inner pages. The outer page should always display the page title for the inner page (it makes sense in my application, perhaps less so in this stripped-down example). This works without any problem in AppJS, but I've been requested to make this app work directly in the browser. I'm getting the error "Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.".

我认为这是由于 Chrome 对本地文件的同源策略,但这并没有帮助我直接解决问题.我可以通过使用每个 规避同源策略的方法.然而,除了这个例子之外,我还想从外页操作内页的 DOM,因为这将使我的代码更清晰 - 因此发布消息不会完全完成这项工作.

I think this is due to Chrome's same origin policy regarding local files, but that hasn't helped me fix the problem directly. I can work around the issue in this stripped-down example by using the window.postMessage method per Ways to circumvent the same-origin policy. However, going beyond this example, I also want to manipulate the DOM of the inner page from the outer page, since this will make my code much cleaner - so posting messages won't quite do the job.

外页

<!DOCTYPE html> <html> <head> <meta name="viewport"> </head> <body> This text is in the outer page <iframe src="html/Home.html" seamless id="PageContent_Iframe"></iframe> <script src="./js/LoadNewPage.js"></script> </body> </html>

内页

<!DOCTYPE html> <html> <head> <title id="Page_Title">Home</title> <meta name="viewport"> </head> <body> This text is in the inner page </body> </html>

JavaScript

var iFrameWindow = document.getElementById("PageContent_Iframe").contentWindow; var pageTitleElement = iFrameWindow.$("#Page_Title");

每 是吗当 iFrame 从本地 html 文件加载本地 html 文件时,Chrome 的未来版本可能支持 contentWindow/contentDocument?,我尝试使用标志启动 Chrome

Per Is it likely that future releases of Chrome support contentWindow/contentDocument when iFrame loads a local html file from local html file?, I tried launching Chrome with the flag

--allow-file-access-from-files

但结果没有变化.

根据 在 Chrome 中禁用同源策略,我尝试使用标志启动 Chrome

Per Disable same origin policy in Chrome, I tried launching Chrome with the flag

--disable-web-security

但结果还是没有变化.

Per document.domain = document.domain 有什么作用?,我让两个页面都运行了命令

Per What does document.domain = document.domain do?, I had both pages run the command

document.domain = document.domain;

这导致错误阻止了一个原点为null"的框架访问原点为null"的框架.请求访问的框架将document.domain"设置为",但被访问的框架确实如此不是.两者都必须将document.domain"设置为相同的值才能允许访问."

为了好玩,我让两个页面都运行命令

For fun, I had both pages run the command

document.domain = "foo";

这导致了错误Uncaught Error: SecurityError: DOM Exception 18".

我正在挣扎.来自更多知识渊博的人的任何帮助都会很棒!谢谢!

I'm floundering. Any help from more knowledgeable people would be fantastic! Thanks!

推荐答案

根据我们一分钟前在我的立方体中的讨论 :)

Per our discussion in my cube just a minute ago :)

我遇到了同样的问题(来自 express 的 Ajax 发布响应Node.js 不断抛出错误) 试图让 AJAX 发布请求正常工作.

I hit this same problem (Ajax post response from express js keeps throwing error) trying to get an AJAX post request to work correctly.

让我绕过它的不是直接从文件系统运行文件,而是从本地服务器运行它.我使用 node 来运行 express.js.您可以使用以下命令安装 express:npm install -g express

What got me around it is not running the file directly off the file system but instead running it from a local server. I used node to run express.js. You can install express with the following command: npm install -g express

完成后,您可以使用以下命令创建一个 express 项目:express -s -e expressTestApp

Once that is accomplished, you can create an express project with the following command: express -s -e expressTestApp

现在,在该文件夹中,您应该看到一个名为 app.js 的文件和一个名为 public 的文件夹.将您要使用的 html 文件放在公共文件夹中.我用以下代码替换了文件 app.js:

Now, in that folder, you should see a file named app.js and a folder named public. Put the html files you wish to work with in the public folder. I replaced the file app.js with the following code:

var express = require('/usr/lib/node_modules/express'); var app = express(); app.use(function(err, req, res, next){ console.error(err.stack); res.send(500, 'Something broke!'); }); app.use(express.bodyParser()); app.use(express.static('public')); app.listen(5555, function() { console.log("Server is up and running"); });

注意,require 行可能不同.您必须找到系统实际放置快递的位置.您可以使用以下命令执行此操作:sudo find/-name express

Note, the require line may be different. You have to find where your system actually put express. You can do that with the following command: sudo find / -name express

现在,使用以下命令启动快速 Web 服务器:node app.js

Now, start the express web server with the following command: node app.js

此时,网络服务器已启动并正在运行.继续并打开浏览器并导航到您的 IP 地址(或者如果您与服务器在同一台机器上,则为 127.0.0.1).使用ip地址:portnumberfilename.html,其中端口号是我们创建的app.js文件中的5555.

At this time, the webserver is up and running. Go ahead and open a browswer and navigate to your ip address (or if you're on the same machine as your server, 127.0.0.1). Use the ip address:portnumberfilename.html where port number is the 5555 in the app.js file we created.

现在在该浏览器中,您不应该(并且在我们测试它时也没有)遇到任何这些相同的问题.

Now in that browser, you shouldn't (and didn't when we tested it) have any of these same problems anymore.

更多推荐

在 Chrome 中对本地文件使用 iframe

本文发布于:2023-10-30 03:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中对   本地文件   Chrome   iframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!