简体   繁体   中英

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...

<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.

Problem: the JavaScript in the colorCombos.php page does not alert, either on page load, or when clicking the button. If I view the page on its own it works fine.

Specs: php5, using Chrome browser.

Of note: there are no errors when I view the javascript console. Also there is nothing else on the colorCombos.php page. 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. Try adding quotes around all the values of each HTML Attribute.

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. 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.

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. For example, I renamed it testcode.php and it worked fine.

I'm still interested in why/how this happened, but at least the pressure is off now that it's working for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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