HighDots Forums  

Javascript innerHTML

Javascript JavaScript language (comp.lang.javascript)


Discuss Javascript innerHTML in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Chris
 
Posts: n/a

Default Javascript innerHTML - 09-23-2003 , 08:00 PM






How can I get the innerHTML of a <div> area only when the page loads, then use that variable in a function?

Here is my code:
function setContent(zz)
{
var lb = document.getElementById('leftbar').innerHTML;
var rb = document.getElementById('rightbar').innerHTML;
document.getElementById("myContent").innerHTML = "<span class=\"title_Page\">"+Page[zz]+"</span>";
if (zz=="home") {
document.getElementById('leftbar').innerHTML = lb;
document.getElementById('rightbar').innerHTML = rb;
document.getElementById('leftbar').style.width = '135px';
document.getElementById('rightbar').style.width = '135px';
}
else {
document.getElementById('leftbar').innerHTML = "";
document.getElementById('rightbar').innerHTML = "";
document.getElementById('leftbar').style.width = '0px';
document.getElementById('rightbar').style.width = '0px';
}
}

I want lb and rb to be set only once (i.e. only when the page loads the first time). I tried putting those variables outside the function, but when I do they, they show up as undefined. So how could i code it so that when the page loads, it assigns the innerHTML to the two variables, but not any other time while that page is open?

Chris

--
Composed with Newz Crawler 1.4 http://www.newzcrawler.com/

Reply With Quote
  #2  
Old   
HikksNotAtHome
 
Posts: n/a

Default Re: Javascript innerHTML - 09-23-2003 , 09:53 PM






In article <z06cb.23390$JM.10393970 (AT) news4 (DOT) srv.hcvlny.cv.net>, "Chris"
<nyk52687 (AT) yahoo (DOT) com> writes:

Quote:
How can I get the innerHTML of a <div> area only when the page loads, then
use that variable in a function?

Here is my code:
function setContent(zz)
{
var lb = document.getElementById('leftbar').innerHTML;
var rb = document.getElementById('rightbar').innerHTML;
Remove the above two lines from the setContent function.

Use the bodys onload even to trigger a function that will set the global
variables to what you want.

var lb,rb;
function pageLoaded(){
lb = document.getElementById('leftbar').innerHTML;
rb = document.getElementById('rightbar').innerHTML;
}
<body onload="pageLoaded()">
Now, you can use the global lb and rb in your second function.
--
Randy


Reply With Quote
  #3  
Old   
Chris
 
Posts: n/a

Default Re: Re: Javascript innerHTML - 09-24-2003 , 05:53 AM



Quote:
In article <z06cb.23390$JM.10393970 (AT) news4 (DOT) srv.hcvlny.cv.net>, "Chris"
nyk52687 (AT) yahoo (DOT) com> writes:

How can I get the innerHTML of a <div> area only when the page loads, then
use that variable in a function?

Here is my code:
function setContent(zz)
{
var lb = document.getElementById('leftbar').innerHTML;
var rb = document.getElementById('rightbar').innerHTML;

Remove the above two lines from the setContent function.

Use the bodys onload even to trigger a function that will set the global
variables to what you want.

var lb,rb;
function pageLoaded(){
lb = document.getElementById('leftbar').innerHTML;
rb = document.getElementById('rightbar').innerHTML;
}
body onload="pageLoaded()"
Now, you can use the global lb and rb in your second function.
--
Randy
When I do that, both variables show undefined.

Chris

--
Composed with Newz Crawler 1.4 http://www.newzcrawler.com/


Reply With Quote
  #4  
Old   
HikksNotAtHome
 
Posts: n/a

Default Re: Javascript innerHTML - 09-24-2003 , 06:37 PM



In article <EIecb.1683$XF.748596 (AT) news4 (DOT) srv.hcvlny.cv.net>, "Chris"
<nyk52687 (AT) yahoo (DOT) com> writes:

<snip>

Quote:
When I do that, both variables show undefined.
Test page online? I tested it in IE6 just to confirm my beliefs.

var someVar = "Not loaded";

function checkFunction(){
alert(someVar)
}
function pageLoaded(){
someVar = "Loaded";
}

<body onload="pageLoaded()">

<button onclick="checkFunction()">Check the Var</button>

The alert I get is "Loaded". Sounds like you are trying to call something
before it exists, hence the request to see a test page.
--
Randy


Reply With Quote
  #5  
Old   
Chris
 
Posts: n/a

Default Re: Re: Javascript innerHTML - 09-24-2003 , 07:41 PM



Quote:
In article <EIecb.1683$XF.748596 (AT) news4 (DOT) srv.hcvlny.cv.net>, "Chris"
nyk52687 (AT) yahoo (DOT) com> writes:

snip

When I do that, both variables show undefined.

Test page online? I tested it in IE6 just to confirm my beliefs.

var someVar = "Not loaded";

function checkFunction(){
alert(someVar)
}
function pageLoaded(){
someVar = "Loaded";
}

body onload="pageLoaded()"

button onclick="checkFunction()">Check the Var</button

The alert I get is "Loaded". Sounds like you are trying to call something
before it exists, hence the request to see a test page.
--
Randy
I've tested it on IIS. As for calling something before it exists, would you recommend I put the function in the page header, an external js file, or somehwere in the <body>?

Thanks, Chris

--
Composed with Newz Crawler 1.4 http://www.newzcrawler.com/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.