/    Sign up×
Community /Pin to ProfileBookmark

Loading iframe src with variable contents

I am trying to find a way to load an iframe’s src attribute with a JavaScript variable that contains a complete HTML page as opposed to a reference to a URI containing a complete HTML page.

I was thinking there may be a way to create and populate an object that mimics a URI and providing that as the source to the iframe but I don’t know how to achieve this.

Sorry if this has been asked before but I did a search and could not find the answer.

Any Ideas?

Thanks,
Nick S.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMay 05.2004 — Hi!

You could use a div instead of an iframe and then say something like: document.getElementById('idGoesHere').innerHTML=yourVariable

Cheers - Pit
Copy linkTweet thisAlerts:
@nicksauthorMay 05.2004 — Hi Pit,

Thanks for your suggestion, it works just fine, but unfortunately, I require the data to be populated within an iframe, not a div.

I tried changing the div to iframe but got an error stating innerHtml is not a property of iframe.

I then tried replacing innerHtml with dataSrc and src, no errors this time, but no data being displayed in iframe either!

Any other ideas?

Thanks,

Nick S.
Copy linkTweet thisAlerts:
@PittimannMay 05.2004 — Hi!

This example will cause an error in Mozilla, if the user refreshes the page after loading. Otherwise it works in IE and Mozilla:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var myvar='<span style="color: red;">xxxxxxxxxx</span>';
//-->
</script>
</head>
<body>
<iframe src="javascript:parent.frames.myvar"></iframe>
</body>
</html>
[/code]

Please remove the space between "java" and "script" in the src...

Cheers - Pit
Copy linkTweet thisAlerts:
@nicksauthorMay 05.2004 — Hi Pit,

That is exactly what I needed and it works exactly as I expected. And I don't even have to filter out the pop-up code anymore!

Loading the iframe using this method seems to ignore any javascript in that variable which is exactly what I wanted.

Thank you very much for your expert (not to mention quick!)help Pit, you guys are great, in fact this entire forum is terrific.

Thanks,

Nick S.
Copy linkTweet thisAlerts:
@PittimannMay 05.2004 — Hi!

You're welcome! ?

Cheers - Pit
×

Success!

Help @nicks 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.6,
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,
)...