/    Sign up×
Community /Pin to ProfileBookmark

Referencing a variable in an iframe

I can’t seem to reference a variable in an iframe.

I have to have an html call the script that writes the iframe though..

My code is like this.

Original HTML:

<SCRIPT language=”JavaScript” SRC=”iframe.js”></SCRIPT>
<SCRIPT>
<!–
var myVariable = window.frames.myFrame.testVar;
alert(myVariable);
–>
</SCRIPT>

IFRAME.JS

<!–

var iframeHTML = ”;
iframeHTML += ‘<iframe id=”myFrame” src=”iframeHTML.html”></iframe>’;
document.write(iframeHTML);

//–>

iframeHTML.html

<script language=”JavaScript”>
var testVar = “hello”;
</script>

Help! ?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Nov 18.2004 — This worked for me. It shouldn't matter if you are using document.write to create the IFrame.

------- Main document ---------

<HTML>

<Head>

<Script Language=JavaScript>

function getVariable(){

alert(window.frames['myFrame'].myVariable)
}


</Script>

</Head>

<Body>

<IFrame

Name = 'myFrame'

Src = 'iframeHTML.html'>

</IFrame>

<br><br>

<input type=button value='Show Variable' onclick="getVariable()">

</Body>

</HTML>

-------- iframeHTML.html --------------

<HTML>

<Head>

<Script Language=JavaScript>

var myVariable = "Hello"

</Script>

</Head>

<Body>

<p> This is the content</p>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@eewaldauthorNov 18.2004 — Thanks, That worked.

I have another problem thouhg. My intention is to determine whether the iframe.html exists.

I cant have a user action initiate the variable call. I need it to detect that variable initially. After I write the frame, if I call just getVariable(); I get an undefined, however if I use a setTimeout('getVariable',0); it seems to work.

Thanks alot for the help.. any ideas why the above is happening?
Copy linkTweet thisAlerts:
@Warren86Nov 18.2004 — You can try using this to check if the IFrame src is fully loaded.

function checkState(){

if (window.frames['myFrame'].document.readyState == 'complete'){doSomething()}

else {setTimeout("checkState()",2000)}

}
Copy linkTweet thisAlerts:
@eewaldauthorNov 18.2004 — Thats perfect. Thanks!!!
Copy linkTweet thisAlerts:
@Warren86Nov 18.2004 — You're welcome. Good luck with your project.
×

Success!

Help @eewald spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...