473,395 Members | 1,383 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,395 software developers and data experts.

Javascript Switch

I have a javascript that displays the site contents and it is included
in every one of my pages. It has logic that makes it work a little
differently in the home page.

Currently I check the page title and if it the home page title I do one
thing otherwise I do another. I wanted to add a translator for the page
which changes the page title.

I would like to get away from using the page title as a check because
of this. I have tried placing a global variable in a separate script on
the home page and it works fine for the home page. The problem is that
it is undefined on the other pages.

Is there a way to check for the presence of a variable without giving
an error if not there? I don't want to define it on every page unless I
have to.

BTW: If I have a script like this: <script src="xxx"
type="text/javascript">some code</script>. Will "some code" be
processed before source code, after source code, or not at all. Is it
consistant on all browsers?

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #1
6 1593
> Is there a way to check for the presence of a variable without giving
an error if not there? I don't want to define it on every page unless I
have to.
Use the typeof operator to check and see if your variable is defined.

example:

if (typeof(myVar) != 'undefined'){
//do processing
}


BTW: If I have a script like this: <script src="xxx"
type="text/javascript">some code</script>. Will "some code" be
processed before source code, after source code, or not at all. Is it
consistant on all browsers?


All browsers as far as I know parse the document in top down order. So if
you want some script to execute before the HTML source is parsed simply
place the script block above the <html> tag.

Regards
Mike
Jul 20 '05 #2
Bonjour à Mike <no****@please.com> qui nous a écrit :
Is there a way to check for the presence of a variable without giving
an error if not there? I don't want to define it on every page
unless I have to.


Use the typeof operator to check and see if your variable is defined.

example:

if (typeof(myVar) != 'undefined'){
//do processing
}


Better (IMHO) :

if (typeof(window['myVar') != 'undefined'){
//do processing
}

--
Cordialement, Thierry ;-)

Jul 20 '05 #3
On Thu, 04 Mar 2004 07:09:47 -0800, Dennis M. Marks
<de******@domain.invalid> wrote:

[snip]
Is there a way to check for the presence of a variable without
giving an error if not there? I don't want to define it on every
page unless I have to.
function isDefined( ref ) {
return 'undefined' != typeof ref;
}
...
if( isDefined( aVariable )) {
...
}
BTW: If I have a script like this: <script src="xxx"
type="text/javascript">some code</script>. Will "some code" be
processed before source code, after source code, or not at all. Is
it consistant on all browsers?


If you specify the source attribute, there should be nothing within the
element. From the HTML 4.01 Specification, Section 18.2.1 - The SCRIPT
element:

"If the src attribute is not set, user agents must interpret the
contents of the element as the script. If the src has a URI value,
user agents must ignore the element’s contents and retrieve the
script via the URI."

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
Dennis M. Marks wrote:
I have a javascript that displays the site contents and it is included
in every one of my pages. It has logic that makes it work a little
differently in the home page.

Currently I check the page title and if it the home page title I do one
thing otherwise I do another. I wanted to add a translator for the page
which changes the page title.

I would like to get away from using the page title as a check because
of this. I have tried placing a global variable in a separate script on
the home page and it works fine for the home page. The problem is that
it is undefined on the other pages.
Check window/document.location.href and the title won't matter.

Is there a way to check for the presence of a variable without giving
an error if not there? I don't want to define it on every page unless I
have to.

BTW: If I have a script like this: <script src="xxx"
type="text/javascript">some code</script>. Will "some code" be
processed before source code, after source code, or not at all. Is it
consistant on all browsers?


Not at all unless the browser doesn't understand the src attribute.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 20 '05 #5
In article <c2*********@news-reader2.wanadoo.fr>, Cenekemoi
<tb***************@harrysoftware.ENVELERcom> wrote:
Bonjour à Mike <no****@please.com> qui nous a écrit :
Is there a way to check for the presence of a variable without giving
an error if not there? I don't want to define it on every page
unless I have to.


Use the typeof operator to check and see if your variable is defined.

example:

if (typeof(myVar) != 'undefined'){
//do processing
}


Better (IMHO) :

if (typeof(window['myVar') != 'undefined'){
//do processing
}

Thank both of you for your help but I couldn't do what I wanted to
anyway.

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #6
Cenekemoi wrote:
if (typeof(window['myVar') != 'undefined'){ ^^^^^^^^^^^^^^^^ //do processing
}


Since "typeof" is an operator and not a method, the parantheses
are not required, (white)space suffices. Besides, there is a
nesting error in the code, it cannot work this way.
PointedEars
Jul 23 '05 #7

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

Similar topics

2
by: Vince C. | last post by:
Hi all. I'm trying to set a cookie expiry date but my script is JS (JavaScript). I've tried Response.Cookies("Test").Expires = Date(); Response.Cookies("Test").Expires =...
5
by: TrvlOrm | last post by:
Can any one please help me...I am new to JavaScript and I have been struggling with this code for days now and can't figure it out. I would like to get the Buttons to correspond with the action...
2
by: bbcrock | last post by:
I am testing in IE7. I display one of several spans when the user mouses over some code. Inside the span are several tabs. Clicking on the tab triggers a JS function that hides the "current"...
7
by: andrewfsears | last post by:
I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)? Let's say that I have a class...
12
by: Manfred Kooistra | last post by:
I have a problem with some JavaScript code not working. I'm sure I've done something obviously stupid, but I can't for the live of me figure it out. Can someone please help? This is the XHTML...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
2
by: smartic | last post by:
i'm having three lists when i select from any one the others be visible by hierarchy but it takes to long to write my code is there is another away to write this code like XML that is my javascript...
19
by: David Given | last post by:
I have a situation where I need to use GOTO in a Javascript program. No, I can't use break or continue. Yes, I really do know what I'm doing. What I've got is a huge mass of automatically...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.