简体   繁体   English

从iframe中执行javascript

[英]Executing javascript from within an iframe

I have: 我有:

  1. A web server (server 1) Web服务器(服务器1)
  2. An application server running some beast of a legacy web app (server 2) 运行旧版Web应用程序的某些野兽的应用程序服务器(服务器2)
  3. An iframe on server 1 pulling in the application from server 2 服务器1上的iframe从服务器2引入应用程序

My problem is: 我的问题是:

The legacy app uses JS validation on its forms. 旧版应用程序在其表单上使用JS验证。 When a user attempts to submit an incomplete form, an alert pops up to notify the user that they are a dummy. 当用户尝试提交不完整的表单时,会弹出警报,通知用户他们是假人。 Of course, this fails when the app is run inside of an iframe because server 1 and server 2 live at different domains. 当然,当应用程序在iframe中运行时,此操作将失败,因为服务器1和服务器2位于不同的域。

I tried setting the following proxy directives on server 1: 我尝试在服务器1上设置以下代理指令:

ProxyPass /legacy_app http://server2.url/legacy_app
ProxyPassReverse /legacy_app http://server2.url/legacy_app

I'm now able to serve the iframe from http://server1.url/legacy_app , but I'm still unable to execute javascript inside that iframe -- I get the same security/access errors as I did when the app was running on a different domain. 现在,我可以从http://server1.url/legacy_app服务iframe,但仍无法在该iframe中执行javascript -与运行该应用程序时遇到的安全/访问错误相同在不同的域上。

Is there something else I can try? 还有什么我可以尝试的吗?

How is the legacy app checking if the boxes are filled in? 旧版应用程序如何检查框是否已填写? Simple javascript? 简单的JavaScript? Ajax? 阿贾克斯?

The alert box itself should still work. 警报框本身仍应工作。 I'm thinking the code for determining if the alert should be issued might be what's broken. 我正在考虑用于确定是否应发出警报的代码可能已损坏。

Running the following code on my local apache server still gives me the alert onLoad even though the page is on a remote host: 即使页面位于远程主机上,在本地apache服务器上运行以下代码仍然会给我onLoad警报:

<html>
    <body>
    <div>
        <iframe src="http://www.crowderassoc.com/javascript/alertbox.html" width="300" height="200">
    </div>
    </body>
</html>

Try copying the above code to a page on server #1 and see if you get the alert box from that remote site in the iframe. 尝试将上述代码复制到服务器#1上的页面上,看看是否从iframe中的该远程站点收到了警告框。

Have you tried hosting the script inside of a .js file hosted on server #1 but running out of the iframe (referenced out of server #2)? 您是否尝试过将脚本托管在服务器#1上托管的.js文件中,但用尽了iframe(服务器2引用了)?

I think a browser is okay with referencing an external site, but doesn't like it when it is referenced by an external site. 我认为浏览器可以引用外部站点,但是当外部站点引用浏览器时,浏览器不喜欢它。

Haven't tried it myself, but I believe that's how I've heard of this sort of a problem being worked around. 我自己还没有尝试过,但是我相信这就是我听说正在解决的此类问题的方式。 I know this is the method that Google Analytics uses - you have to request the .js file from Google's servers, but once it's there, it has access to the browser. 我知道这是Google Analytics(分析)使用的方法-您必须从Google的服务器请求.js文件,但是一旦存在,它就可以访问浏览器。

Joe, I think you are correct. 乔,我认为你是正确的。 A quick test with other servers shows that I can trigger alerts from remotely-hosted scripts quite easily. 对其他服务器的快速测试表明,我可以轻松触发来自远程托管脚本的警报。

The legacy server is the client's and we don't have easy access to it, but glancing at their JS it looks like they're doing some sort of cross-site/framing detection -- worth further investigation. 传统服务器是客户端的服务器,我们无法轻松访问它,但是浏览他们的JS似乎他们在进行某种跨站点/框架检测-值得进一步研究。

I've had this situation in the past where I was trying to build an app around a heavily scripted pre-existing app on a remote server, and the app would run fine if it was opened in its own window, but if I tried loading it into a frame, it would break. 过去我曾遇到过这种情况,当时我试图在远程服务器上围绕大量脚本编写的现有应用程序构建应用程序,如果在自己的窗口中打开该应用程序,它将运行正常,但是如果我尝试加载它变成一个框架,它将破裂。

What I ended up doing for this project was opening the local application in a pop-up with a width of 495px, loading the external app in the main (already existing) window, resizing the main external app window to the screen width minus 495px, and positioning the windows side by side on the screen. 我最终为该项目所做的工作是在一个宽度为495px的弹出窗口中打开本地应用程序,将外部应用程序加载到主(已经存在)窗口中,将外部应用程序主窗口的大小调整为屏幕宽度减去495px,并将窗口并排放置在屏幕上。 This gave the end user a similar effect to what I had been trying to do with frames, only it worked. 这给了最终用户一个与我一直尝试使用框架的效果相似的效果,但只有它起作用了。

In case it helps, here is the code I used from my index.php file: 如果有帮助,这是我在index.php文件中使用的代码:

// Manipulating the current window
window.location.href = 'http://www.someExternalApp.com'; // setting the page location.
window.name = 'legacyapp'; // setting the window name just the for heck of it.
moveTo(0,0); // moving it to the top left.

// Resizing the current window to what I want.
mainWindowWidth = screen.width - 495;
mainWindowHeight = screen.height; // Makes the window equal to the height of the users screen.
resizeTo(mainWindowWidth,mainWindowHeight); 

// function for opening pop-up
function openWin(){
    win2 = window.open(page,'',winoptions);
    win2.focus();
}

// internal app location (for use in pop-up)
page = 'someLocalApp.php';

// internal app Window Options (for pop-up)
winoptions = 'width=490,height='+mainWindowHeight+',top=0,left='+mainWindowWidth+'leftscrollbars=1,scrolling=1,scrollbars=1,resizable=1,toolbar=0,location=0,menubar=0,status=0,directories=0';

// Opens the local app pop-up
openWin();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM