Javascript: Trying to change the text (/ or call a function) in an iFrame, from a JavaScript file
Ok! I figured it out! Thanks alot you guys (thanks algiecas)! The problem was that I wasn't testing it on Localhost, I can just edit the text now, and It's all good! Can't make the function to work it seems.. But I CAN edit the text now in the iFrame, so my problem is solved!
I have searched all over the web, but cant seem to get this working.. I'm trying to use:
-frames["FRAME"].document.getElementById("txtNewsHtm")
-document.getElementById('FRAME').contentWindow.updateNews();
-FRAME.txtNewsHtm.innerHTML = 'test1';
All these ways to try and cha开发者_StackOverflownge a <p id = 'txtNewsHtm'>
inside of an iFrame (home.html), thats included into index.html. I also have a .js file where I keep most of my code.
I've tried to use a function inside of the iFrame (point nr 2), didn't work, i've tried to access the <p>
directly from the .js file (points nr 1 and 3), didn't work. I've tried ALOT of other things as well, but i just can't make it too work.
Does anyone has any advice for me? Please help me out here.
I'm making language buttons for on a website that, through javascript, dynamically change the text on the website, it works for index.html, but I can't seem to get it to work for the iFrame home.html. Seems like home.html doesn't want to 'listen' to the .js file OR index.html.
Thanks!
Try this for a test. In parent .js file create a function
function setFrameHTML() {
window.frames[0].document.getElementById('txtNewsHtm').innerHTML = 'test';
}
This should work.
You might be executing javascript from the page in the frame itself. Try using top instead of window (copying the code from Alex Ackerman):
function setFrameHTML() {
top.frames[0].document.getElementById('txtNewsHtm').innerHTML = 'test';
}
EDIT:
Ok, so after some discussion it turns out, that you need to test you HTML pages from localhost (or remote server), not opening file from the file system (file:///...)
Second thing: you are calling updateNews();
before the function is declared. Move it after the function declaration. The same thing with changeText();
精彩评论