473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 70717
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
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....
4
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
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...
3
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...
2
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...
2
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...
1
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...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.