View Full Version : using a var as src for iframe
lukeprog
03-28-2003, 04:21 PM
In a bunch of Javascript in the <HEAD> of my HTML page, I include the following line (not inside a function, just by itself):
var currentpage="1.html"
Then, in the <BODY> of my HTML page, I have an <iframe> tag that looks like this:
<iframe noresize width=891# height=558# src=currentpage name="page" frameborder="0">
</iframe>
Now, this used to work fine when I had the <iframe> tag looking like this:
<iframe noresize width=891# height=558# src="1.html" name="page" frameborder="0">
</iframe>
But now it doesn't because instead of the src being "1.html", it is now "currentpage" which SHOULD be "1.html" anyway, but for some reason it doesn't work (it loads the standard IE error page inside the iframe when I test it). Why? How do I fix this and make the iframe load the "currentpage"?
joh6nn
03-28-2003, 06:16 PM
it doesn't work, because you're trying to load a javascript variable into plain html.
try it this way:
<iframe noresize width=891# height=558# src="about:blank" name="page" frameborder="0">
</iframe>
<script>self.frame["page"].location="1.html";</script>
Quiet Storm
03-28-2003, 06:21 PM
Could you do it this way?:
<script>
document.write('<iframe noresize width="891" height="558" src="' + currentpage + '" name="page" frameborder="0">
</iframe>
</script>
joh6nn
03-28-2003, 06:33 PM
yep, either way would work. it's just a matter of preference.
cheesebagpipe
03-28-2003, 09:52 PM
<head>
<script type="text/javascript">
var currentpage="1.html"
</script>
</head>
<body>
<iframe noresize width="891" height="558" src="javascript:location=parent.currentpage" name="page" frameborder="0"></iframe>
</body>
joh6nn
03-29-2003, 11:36 PM
that's 1) really clever, Cheesebagpipe. i'd never have thought of that. but, 2) it may need a bit of tweaking, i think. if it doesn't work, try this:
<head>
<script type="text/javascript">
var currentpage="1.html"
</script>
</head>
<body>
<iframe noresize width="891" height="558" src="javascript:self.frames['page'].location.href=parent.currentpage;" name="page" frameborder="0"></iframe>
</body>
cheesebagpipe
03-29-2003, 11:56 PM
Not really - the execution context of a javascript: URL is related to where you place it; in this case, it's 'inside' the iframe 'looking out' at the parent page's data. You scripted it as if it was running in the parent window...here's a clearer example:
<html>
<head>
<script type="text/javascript">
var HTML = '';
function k() {
HTML += '<html><head><title>DEMO Window</title></head>';
HTML += '<body bgcolor="salmon" onload="opener.document.z.zz.value=\'hoohah\'">';
HTML += '</body></html>';
open('javascript:opener.HTML','','width=200,height=200,left=200,top=200,status=0');
}
</script>
</head>
<body>
<form name="z">
<input type="button" value="GO" onclick="k()">
<input name="zz">
</form>
</body>
</html>
You can use this anywhere a src/href is expected - I've even used it to load entire JS files, stored as strings. Very useful. Love those unwanted linebreaks...
joh6nn
03-30-2003, 12:03 AM
yeah, it's just that i don't trust it to work properly across all browsers, so i like to make sure that my references are absolute.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.