473,538 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript innerHTML

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/
Jul 20 '05 #1
4 70732
In article <z0*********************@news4.srv.hcvlny.cv.net >, "Chris"
<ny******@yahoo.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
Jul 20 '05 #2
> In article <z0*********************@news4.srv.hcvlny.cv.net >, "Chris"
<ny******@yahoo.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/
Jul 20 '05 #3
In article <EI******************@news4.srv.hcvlny.cv.net>, "Chris"
<ny******@yahoo.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
Jul 20 '05 #4
> In article <EI******************@news4.srv.hcvlny.cv.net>, "Chris"
<ny******@yahoo.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/
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3713
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button. When the user clicks the "Add another email" it will call a client side JavaScript function, add_email, which will dynamically add a new set of...
4
8834
by: Henry | last post by:
Hi I have a problem, pls see below: <html> : <script type="text/javascript"> <!-- function addCode () { document.getElementById('emptySpace').innerHTML+=
8
3203
by: Pratik Patel | last post by:
Hello, I used innerHTML to assign HTML content. but in my HTML page content have also some javascript function and it will run when page load. bu when HTML code assgin thru innerHTML then this javascript is not working. e.g. html content:
3
4132
by: Tarik Monem | last post by:
Just the other day, I received help from this forum on escape sequences with some PHP code which would echo a Javascript onclick. After the reply helped me, I asked him to explain why it worked and he gave me a URL explaining escape sequences. I thought I understood the tutorial, but I guess I do not. The closest that I have come to making...
2
222
by: sheldonlg | last post by:
Erwin Moller wrote: This goes along with what I have experienced. I recently (a few months bsck) started working in an AJAX framework. I quickly found that if I replaced some php code (while in the app), it was seen immediately by hitting the element that exercised the existing js code to exercise that php code. If, however, I...
2
3721
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm in a gridview edit row that has a DropDownList in one column and a status Label in the 2nd column. When i changed the value in the dropdownlist, there's a client-side javascirpt function that runs and successfully populates label in the 2nd column. Everything is fine at this point and working the way it should. i do a...
1
2561
by: Gino Dicandio | last post by:
Hello, I actually use a counter on a webpage (This is an external script that reads and increments a value - It actually works) To do it, I use an inline javascript but I would like to unify the entire page and call that counter directly in the external Javascript that manages the whole site. Here's the actual code... HTML <body...
2
2413
by: Warren Wyght | last post by:
I would like to display data from a MySQL DB on a PHP page after it has been rendered. I know I could use JavaScript's innerHTML but I don't know the most intelligent method of getting a dataset to the page from the MySQL DB. Is there a "best" way to get a dataset to the page and then have JavaScript parse through it showing one item at a...
0
7365
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7308
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5829
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4852
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3353
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.