Update element

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>
samprgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
set the << iframe >> variable as global for example : http://jsfiddle.net/eSqSj/
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";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GreggCommented:
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

<!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>

Open in new window

0
samprgAuthor Commented:
Awesome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.