473,404 Members | 2,114 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,404 software developers and data experts.

How to declare global variables?

I'm trying to develop a website with a left frame div and a main div, whereby the content of the main div changes depending on what you click on in the frame div. I'm trying to achieve this with onclick, which calls a function setValue(i) and sets the global variable value. From my main div I call a function content() which has if...else statements to display different content depending on the value variable. But for some reason, even though I'm declaring value outside of any functions, when setValue assigns a number to value, it doesn't seem to carry into the function content(). Can anyone help me figure out what I'm doing wrong? I've looked it over and over, and researched online, and can't figure it out. I've just started learning Javascript, so it's probably something really simple. The code is below:

In the <head> section:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var value;
  3. function setValue(i)
  4.   {
  5.   value=i;
  6.   }
  7. function content()
  8.   {
  9.   if (value==1)
  10.     {
  11.     document.write("<a href='cheungfai.html'>");
  12.     document.write("<img src='images/IMG_0171s.jpg'/>");
  13.     document.write("<p class='p3'>");
  14.     document.write("Travel and Nature Portfolio");
  15.     document.write("</p></a>");
  16.     }
  17.   }
  18. </script>
  19.  
In the <body> section:
[HTML]<div class="leftframe">
<p class="p2" onclick="setValue(1)" />
Felix Poon's Photography
</p>
</div>

<div class="maincontent">
<p><script type="text/javascript">
content();
</script></p>
</div>[/HTML]
Sep 25 '08 #1
1 2023
pronerd
392 Expert 256MB
There is nothing wrought with your variable scope. In the code you provided you are never calling the content() function after the onClick event. The content() function is being run one time when the page loads, and is never called again.

You need to call the function you want to run as part of the event it is triggering off of.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <p class="p2" onclick="setValue(1);content();" />
  3.  
  4.  
Sep 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: NotGiven | last post by:
I have an file I call using: require_once() In this file I have variables I'd like to use in the calling page and functions called by that page. How can I do this? example:
10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
4
by: Bill | last post by:
If I declared an array or object in a .JS file, should it not be global throughout all the files which reference it?
6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
19
by: Hal Styli | last post by:
Hello, Can someone please help. I need to use a large array and I'm not sure when one should use (A) and when one should use (B) below:- #define MAX 10000000 (A) int x;
5
by: John Salerno | last post by:
Here's my full code: using System; using System.Collections.Generic; using System.Text; using System.IO; namespace PatternMatcher { class Program
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
9
by: johnlim20088 | last post by:
Hi, Hi, currently I developing an application in C# NET, let say i have a page call viewrecord.aspx, in this page, i have a global variables declared before the page_Load, so all function can...
5
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another...
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?
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...
0
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,...
0
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,...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.