Clicky

Hello,
I have tow Javascript function in html to add website, I need those fonctions use same iframe.

<script language="javascript" type="text/javascript">
// <!CDATA[

function Button1_onclick() {
      var iframe = document.createElement('iframe');
      iframe.src = "http://www.bing.com";
      iframe.style.width = '100%';
      iframe.style.height = "60px";
      iframe.name = "internal";
      iframe.scrolling = "yes";
      iframe.marginwidth="0";
      iframe.frameborder="0";
      iframe.marginheight="0";
      iframe.vspace="0";
      iframe.hspace="0";
      
      document.body.appendChild(iframe);
}
function Button2_onclick() {

      var iframe = document.createElement('iframe');
      iframe.src = "http://www.yahoo.com";
      iframe.style.width = '100%';
      iframe.style.height = "60px";
      iframe.name = "internal";
      iframe.scrolling = "yes";
      iframe.marginwidth="0";
      iframe.frameborder="0";
      iframe.marginheight="0";
      iframe.vspace="0";
      iframe.hspace="0";
      
      document.body.appendChild(iframe);
}


// ]]>
</script>

asked 12/14/2011 08:29

samprg's gravatar image

samprg ♦♦


3 Answers:
set the << iframe >> variable as global for example : http://jsfiddle.net/eSqSj/
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
var iframe = null;

window.onload = function() {
      iframe = document.createElement('iframe');
      document.body.appendChild(iframe);
      iframe.style.width = '100%';
      iframe.style.height = "800px";
      iframe.name = "internal";
      iframe.scrolling = "yes";
      iframe.marginwidth="0";
      iframe.frameborder="0";
      iframe.marginheight="0";
      iframe.vspace="0";
      iframe.hspace="0";
//      iframe.src = "http://www.bing.com";      
      document.body.appendChild(iframe);
}

function Button1_onclick() {
      iframe.src = "http://www.bing.com";
}
function Button2_onclick() {
      iframe.src = "http://www.yahoo.com";
}
link

answered

leakim971's gravatar image

leakim971

A simple fix would to make the variable iframe global. You can accomplish that by placing the declaration of iframe outside of the functions.

See tutorial about variable scope
http://www.mredkj.com/tutorials/reference_js_intro_ex.html

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

//Declare outside of the functions.
var iframe = document.createElement('iframe');

function Button1_onclick() {

     //reference inside of the functions.
      iframe = document.createElement('iframe');
      iframe.src = "http://www.bing.com";
      iframe.style.width = '100%';
      iframe.style.height = "60px";
      iframe.name = "internal";
      iframe.scrolling = "yes";
      iframe.marginwidth="0";
      iframe.frameborder="0";
      iframe.marginheight="0";
      iframe.vspace="0";
      iframe.hspace="0";
      
      document.body.appendChild(iframe);
}
function Button2_onclick() {

      
      iframe.src = "http://www.yahoo.com";
      iframe.style.width = '100%';
      iframe.style.height = "60px";
      iframe.name = "internal";
      iframe.scrolling = "yes";
      iframe.marginwidth="0";
      iframe.frameborder="0";
      iframe.marginheight="0";
      iframe.vspace="0";
      iframe.hspace="0";
      
      document.body.appendChild(iframe);
}

window.onload = function() {
	Button2_onclick();
};

// ]]>
</script> 
</head>

<body>
</body>
</html>
link

answered 2011-12-14 at 16:38:14

gregg_s's gravatar image

gregg_s

Awesome
link

answered 2011-12-14 at 16:39:46

samprg's gravatar image

samprg

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×104
×125

Asked: 12/14/2011 08:29

Seen: 300 times

Last updated: 12/14/2011 08:48