简体   繁体   English

iframe 中的 javascript 不起作用

[英]javascript in iframe won't work

I have a website with lots of code, so obviously something is wrong, but I can't post all the code here.我有一个包含大量代码的网站,因此显然有些问题,但我无法在此处发布所有代码。 Here's the exact code I'm trying to use though.这是我尝试使用的确切代码

Parent document...父文件...

<iframe name=combosIframe id=combosIframe src=getCombos.php style='position:absolute;left:300px;top:0px;width:275px;height:520px;overflow-y:scroll;overflow-x:hidden;border:1px solid black'></iframe>
<form name=colorForm id=colorForm action=colorCombos.php method=post target=combosIframe>
   <input type=submit id=submitColors value=SubmitColors>
</form>

Inside the iframe, colorCombos.php page...在 iframe 中,colorCombos.php 页面...

<script language=JavaScript>
   alert('load');
   function doSomething(){
      alert('hi');
   }
</script>

<form>
   <input type=button value=ClickMe onClick=doSomething()>
</form>

Scenario: I click the parent form button to load the new page into the iframe.场景:我单击父表单按钮将新页面加载到 iframe 中。

Problem: the JavaScript in the colorCombos.php page does not alert, either on page load, or when clicking the button.问题: colorCombos.php页面中的 JavaScript 不会在页面加载时或单击按钮时colorCombos.php警报。 If I view the page on its own it works fine.如果我自己查看页面,它工作正常。

Specs: php5, using Chrome browser.规格:php5,使用Chrome浏览器。

Of note: there are no errors when I view the javascript console.注意:当我查看 javascript 控制台时没有错误。 Also there is nothing else on the colorCombos.php page. colorCombos.php 页面上也没有其他内容。 Both files are in the same directory.两个文件都在同一个目录下。 Thanks for any help, I'm really stumped.感谢您的帮助,我真的很难过。

Not using quotes within HTML Attributes can have undesired effects.在 HTML 属性中不使用引号可能会产生不良影响。 Try adding quotes around all the values of each HTML Attribute.尝试在每个 HTML 属性的所有值周围添加引号。

Example:例子:

<input type="button" value="ClickMe" onClick="doSomething()"/>

I started with a blank setup (which was working) copied the code from my site (which has lots of other code), bit by bit into the new document to see when the script stopped working.我从一个空白设置(正在运行)开始,从我的站点(其中有很多其他代码)复制代码,一点一点地复制到新文档中,以查看脚本何时停止工作。 I ended up copying EVERYTHING (literally ctrl+a) into the new document, and it worked fine.我最终将所有内容(字面意思是 ctrl+a)复制到新文档中,并且运行良好。 I then renamed the new document to be the name of the old document, and it STOPPED working.然后我将新文档重命名为旧文档的名称,它停止工作。 The old document (ie the parent document) is named rangeTool.php, not that I think that is relevant necessarily.旧文档(即父文档)被命名为 rangeTool.php,我认为这不一定是相关的。

So I have a solution, but not an answer.所以我有一个解决方案,但没有答案。 What in the world could cause one document to not work?到底什么会导致一份文件不起作用? Something wrong on the server?服务器出问题了? I deleted the old file off the server and loaded the new copy, but that didn't work as long as the name was rangeTool.php.我从服务器上删除了旧文件并加载了新副本,但是只要名称是 rangeTool.php,这就不起作用。 For example, I renamed it testcode.php and it worked fine.例如,我将其重命名为 testcode.php 并且运行良好。

I'm still interested in why/how this happened, but at least the pressure is off now that it's working for me.我仍然对为什么/如何发生这种情况感兴趣,但至少现在压力减轻了,因为它对我有用。

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

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