Jump to content

[SOLVED] Creating Iframes Quick


therealwesfoster

Recommended Posts

I have this code

 

function doit()
{
var iframe = document.createElement("iframe");
iframe.id = "gf";
iframe.name = "gf";
iframe.frameborder = 0;
iframe.style.top = '0px';
iframe.style.left = '0px';
iframe.height = '0px';
iframe.width = '0px';
iframe.style.position = "absolute";
iframe.style.display = "block";
iframe.scrolling = "auto";
iframe.src = "http://google.com";
window.document.body.appendChild(iframe);
}

 

It works in mozilla firefox, but doesn't work in IE.. help please :)

Link to comment
https://forums.phpfreaks.com/topic/80154-solved-creating-iframes-quick/
Share on other sites

it worked for me; I did it like this:

 

<html><head></head><body>
<script language="javascript">

function doit()
{
var iframe = document.createElement("iframe");
iframe.id = "gf";
iframe.name = "gf";
iframe.frameborder = 0;
iframe.style.top = '0px';
iframe.style.left = '0px';
iframe.height = '400px';
iframe.width = '500px';
iframe.style.position = "absolute";
iframe.style.display = "block";
iframe.scrolling = "auto";
iframe.src = "http://google.com";
window.document.body.appendChild(iframe);
}

</script>

<a href="javascript:void(0)" onclick="doit()">test</a>

<body></html>

It works for me too in firefox.. but in IE i get a popup error and it says this:

 

Internet Explorer cannot open the internet site http://bla-bla.com/.

 

Operation Aborted.

 

And the error is coming from the iframe javascript file..  ??? confusing? why yes... yes it is

 

with one exception, I had to add this to the function, because it wouldnt auto lookup google

 

document.getElementById('gf').src = "http://www.google.com";

 

 

now that does create an error message:

 

document.getElementById('gf').src has no properties

Thats strange, cause on all these browsers

 

Mac-FireFox 2

Mac-Safari

Mac-Opera

Windows-FireFox2

Windows-IE6

Windows-Safari

 

I get a nice frame with google showing up inside

 

so here is the code so you can test what i tested

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>

<body>
<script language="javascript">
function doit()
{
var iframe = document.createElement("iframe");
iframe.id = "gf";
iframe.name = "gf";
iframe.frameborder = 0;
iframe.style.top = '0px';
iframe.style.left = '0px';
iframe.height = '400px';
iframe.width = '500px';
iframe.style.position = "absolute";
iframe.style.display = "block";
iframe.scrolling = "auto";
//iframe.src = "http://www.google.com";
window.document.body.appendChild(iframe);
document.getElementById('gf').src = "http://www.google.com";
}

</script>

<a href="javascript:void(0)" onclick="doit();">test</a>
</body>
</html>

 

If your getting an error, i would check your script settings or run spybot or adaware

Thats strange, cause on all these browsers

 

Mac-FireFox 2

Mac-Safari

Mac-Opera

Windows-FireFox2

Windows-IE6

Windows-Safari

 

I get a nice frame with google showing up inside

 

so here is the code so you can test what i tested

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>

<body>
<script language="javascript">
function doit()
{
var iframe = document.createElement("iframe");
iframe.id = "gf";
iframe.name = "gf";
iframe.frameborder = 0;
iframe.style.top = '0px';
iframe.style.left = '0px';
iframe.height = '400px';
iframe.width = '500px';
iframe.style.position = "absolute";
iframe.style.display = "block";
iframe.scrolling = "auto";
//iframe.src = "http://www.google.com";
window.document.body.appendChild(iframe);
document.getElementById('gf').src = "http://www.google.com";
}

</script>

<a href="javascript:void(0)" onclick="doit();">test</a>
</body>
</html>

 

If your getting an error, i would check your script settings or run spybot or adaware

 

no, I do not have spyware or adware - have great software for that stuff; you just did not specify where to put your code.

 

I was testing your code like; this:

 

<script language="javascript">
function doit()
{
var iframe = document.createElement("iframe");
iframe.id = "gf";
iframe.name = "gf";
iframe.frameborder = 0;
iframe.style.top = '0px';
iframe.style.left = '0px';
iframe.height = '400px';
iframe.width = '500px';
iframe.style.position = "absolute";
iframe.style.display = "block";
iframe.scrolling = "auto";
document.getElementById('gf').src = "http://www.google.com";
window.document.body.appendChild(iframe);
}

</script>

 

 

that is why I was getting the error message. But again; the original code worked fine for me; when I tested it in IE7 & FF.

Well i kept getting errors.. and then it stopped working all together.. lol, i dont know why

 

but I recoded it to this and it works like magic :)

 

<html>
<head>
<script language="JavaScript" type="text/javascript">
function makeFrame() {
   ifrm = document.createElement("IFRAME");
   ifrm.setAttribute("src", "http://google.com/");
   ifrm.style.width = 640+"px";
   ifrm.style.height = 480+"px";
   document.body.appendChild(ifrm);
}
</script>
</head>
<body>
<p><a href="#" onMouseDown="makeFrame()">GO! </a></p>
</body>
</html> 

just to followup:

 

sorry, i assumed you would put line (1) after line (2) because you cant reference an object until it is instantiated

(1) document.getElementById('gf').src = "http://www.google.com";
(2) window.document.body.appendChild(iframe);

 

Man I hardly ever put JS in the header anymore, due to IE, In IE one main rule applies with JS, if the code is placed prior to the object, your lucky if it works ;-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.