473,796 Members | 2,864 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.getEle mentById('leftb ar').innerHTML;
var rb = document.getEle mentById('right bar').innerHTML ;
document.getEle mentById("myCon tent").innerHTM L = "<span class=\"title_P age\">"+Page[zz]+"</span>";
if (zz=="home") {
document.getEle mentById('leftb ar').innerHTML = lb;
document.getEle mentById('right bar').innerHTML = rb;
document.getEle mentById('leftb ar').style.widt h = '135px';
document.getEle mentById('right bar').style.wid th = '135px';
}
else {
document.getEle mentById('leftb ar').innerHTML = "";
document.getEle mentById('right bar').innerHTML = "";
document.getEle mentById('leftb ar').style.widt h = '0px';
document.getEle mentById('right bar').style.wid th = '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 70758
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.getEle mentById('leftb ar').innerHTML;
var rb = document.getEle mentById('right bar').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.getEle mentById('leftb ar').innerHTML;
rb = document.getEle mentById('right bar').innerHTML ;
}
<body onload="pageLoa ded()">
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.getEle mentById('leftb ar').innerHTML;
var rb = document.getEle mentById('right bar').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.getEle mentById('leftb ar').innerHTML;
rb = document.getEle mentById('right bar').innerHTML ;
}
<body onload="pageLoa ded()">
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.sr v.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="pageLoa ded()">

<button onclick="checkF unction()">Chec k 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.sr v.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="pageLoa ded()">

<button onclick="checkF unction()">Chec k 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
3736
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 controls to the webpage using the innerHTML method. It appears to work perfectly fine in the browser. The...
4
8851
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
3220
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
4146
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 this work, is the following code: myDiv.innerHTML += '<td><a...
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 replaced the code that contained the javascript, it was not seen until I reloaded the
2
3737
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 getElementById to get the status label and then i set its innerHTML and again it works fine.
1
2577
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 onLoad=gen_hits()> ...
2
2433
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 time? I can parse through the set but don't know what I should use to hold the set. There will be...
0
10453
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6785
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.