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

Why won't this js work properly?

In the following, my first attempt at using hash array, if 'score' == 0 or
'undefined' the following script fails with the js-console message:
'Could not convert undefined or null to object
document.getElementById(target1).innerHTML = regOut;
decodeRegion(0, "target")'

var region = new Array();
region["0"]="an unspecified region";
region["undefined"]="an unspecified region";
region["11"]="the upper-outer quadrant";
etc.

function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
if (score > 100) {regOut1 = ' and other'; score -= 100;}
regOut = region[score] ;
document.getElementById(target).innerHTML = regOut;
return;
}

** If I don't escape, I get the error.

I've tried things like if(score == 0) {regOut = 'somestring;} after
regOut=region[score] , and also guaranteeing that score is a string using
score.toString(10), but it still doesn't play.

--
Ed Jay (remove M to respond by email)
Dec 27 '05 #1
11 1412

Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined
document.getElementById(target).innerHTML = regOut; From the error message you received, I would say your problem lies right on this line. And also judging from your error message, you have
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)
{
if(target)
{
document.getElementById(target).innerHTML = regOut;
}
}
return;


This is just a personal preference, but in this case I don't see a need
to return undefined. Does your function really need to return
undefined? Or once you return undefined, will you be using that value
somewhere?

Dec 27 '05 #2
"web.dev" <we********@gmail.com> wrote:

Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined


OK.
document.getElementById(target).innerHTML = regOut;
From the error message you received, I would say your problem lies

right on this line. And also judging from your error message, you have
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:


I'm using <span id="target"></span> for all my placeholders. I've checked
(carefully) and they're all there.

As an aside...is there a limit to 'target.length?'

Also, I'm calling the function ten times with different scores and targets
using <body onLoad="....>. Is it possible there's a timing issue?
if(document.getElementById)
{
if(target)
{
document.getElementById(target).innerHTML = regOut;
}
}
return;


This is just a personal preference, but in this case I don't see a need
to return undefined. Does your function really need to return
undefined? Or once you return undefined, will you be using that value
somewhere?


I automatically use it as an escape. It's no big deal.

--
Ed Jay (remove M to respond by email)
Dec 27 '05 #3
web.dev wrote:
Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined


No, it is far more reliable to compare

typeof score == "undefined"

And it is more efficient to test

if (typeof score == 'undefined' || score == 0)
{
return;
}

because if the type is undefined, it is not necessary
to check for the value. Even more efficient would be

if (!score)
{
return;
}

but that would apply to any false-value.
document.getElementById(target).innerHTML = regOut;

From the error message you received, I would say your problem lies

right on this line. And also judging from your error message, you have


Please learn how to post. Quoted text should be clearly distinguishable
to your text.
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)
That is testing for value, not functionality.

<URL:http://pointedears.de/scripts/test/whatami#inference>
{
if(target)
{
document.getElementById(target).innerHTML = regOut;


And that is error-prone nonsense.

The _minimum_ required for this not to break with certain values is

function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (isMethodType(typeof document.getElementById))
{
var o;
if ((o = document.getElementById(target)))
{
o.innerHTML = regOut;
}
}
PointedEars
Dec 28 '05 #4
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
web.dev wrote:
Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined


No, it is far more reliable to compare

typeof score == "undefined"

And it is more efficient to test

if (typeof score == 'undefined' || score == 0)
{
return;
}

because if the type is undefined, it is not necessary
to check for the value. Even more efficient would be

if (!score)
{
return;
}

but that would apply to any false-value.
document.getElementById(target).innerHTML = regOut;

From the error message you received, I would say your problem lies

right on this line. And also judging from your error message, you have


Please learn how to post. Quoted text should be clearly distinguishable
to your text.
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)


That is testing for value, not functionality.

<URL:http://pointedears.de/scripts/test/whatami#inference>
{
if(target)
{
document.getElementById(target).innerHTML = regOut;


And that is error-prone nonsense.


Why is it error prone?

Taking your lead from above, how about:

if (!score || !target)
{
return;
}

which appears to work without error.
The _minimum_ required for this not to break with certain values is

function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (isMethodType(typeof document.getElementById))
{
var o;
if ((o = document.getElementById(target)))
{
o.innerHTML = regOut;
}
}

Thank you for your comments.

--
Ed Jay (remove M to respond by email)
Dec 28 '05 #5
Thomas 'PointedEars' Lahn said the following on 12/27/2005 7:09 PM:

<snip>

The _minimum_ required for this not to break with certain values is
No, it is not the "minimum required for this not to break".
function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (isMethodType(typeof document.getElementById))
{
var o;
if ((o = document.getElementById(target)))
{
o.innerHTML = regOut;


And if the browser doesn't support innerHTML? You use the feature but do
not test for it.

The only reliable way to test for innerHTML support, to date, is to
insert non-normalized HTML into a container using innerHTML and then
read it back. If it is normalized, the browser supports innerHTML. If it
is not normalized, then the browser doesn't support innerHTML.

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 28 '05 #6
On 2005-12-27, Ed Jay <ed***@aes-intl.com> wrote:
In the following, my first attempt at using hash array, if 'score' == 0
javascript does not have "hash" or "associative" arrays, you code as adding
properties to an object or elements to an array depending on the index value
you use..
or
'undefined' the following script fails with the js-console message:
'Could not convert undefined or null to object
document.getElementById(target1).innerHTML = regOut; decodeRegion(0, "target")'

var region = new Array();
region[0]="OOPS!"; // see if that makes any difference.
region["0"]="an unspecified region";
region["undefined"]="an unspecified region";
region["11"]="the upper-outer quadrant";
etc.



--

Bye.
Jasen
Dec 28 '05 #7
Jasen Betts <ja***@free.net.nospam.nz> wrote:
On 2005-12-27, Ed Jay <ed***@aes-intl.com> wrote:
In the following, my first attempt at using hash array, if 'score' == 0
javascript does not have "hash" or "associative" arrays, you code as adding
properties to an object or elements to an array depending on the index value
you use..


A rose, by any other name... :-) (My books use the term hash array and
associative array interchangeably.)
or
'undefined' the following script fails with the js-console message:
'Could not convert undefined or null to object
document.getElementById(target1).innerHTML = regOut;
decodeRegion(0, "target")'

var region = new Array();


region[0]="OOPS!"; // see if that makes any difference.


Sure does...stopped it from working.
region["0"]="an unspecified region";
region["undefined"]="an unspecified region";
region["11"]="the upper-outer quadrant";
etc.


--
Ed Jay (remove M to respond by email)
Dec 28 '05 #8
Randy Webb wrote on 28 dec 2005 in comp.lang.javascript:
And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.


Like this?

<div id=d>Hello world</div>

<script type='text/javascript'>

var d = document.getElementById('d')
d.innerHTML='<br>'
if (d.innerHTML==='<BR>') // test for uppercase!
alert('normalized innerHTML')

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 28 '05 #9
Evertjan. said the following on 12/28/2005 5:02 AM:
Randy Webb wrote on 28 dec 2005 in comp.lang.javascript:

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.
Like this?

<div id=d>Hello world</div>

<script type='text/javascript'>

var d = document.getElementById('d')
d.innerHTML='<br>'


I think it used something more along the lines of:

newHTML = '<s Pa N>SomeText</ s PA n>';

And then checked to see if the new innerHTML.toLowerCase() was equal to
newHTML.toLowerCase() to see whether it normalized the spaces out of the
tags.
if (d.innerHTML==='<BR>') // test for uppercase!
alert('normalized innerHTML')


Yes, that is the basic idea :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 28 '05 #10
JRS: In article <_L********************@comcast.com>, dated Tue, 27 Dec
2005 21:31:26 local, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :

The only reliable way to test for innerHTML support, to date, is to
insert non-normalized HTML into a container using innerHTML and then
read it back. If it is normalized, the browser supports innerHTML. If it
is not normalized, then the browser doesn't support innerHTML.

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.

(1) Is it possible for a browser to support innerHTML as seen from the
scripting side, and yet not to make the corresponding effect on its
visible screen?

(2) Could you get your grammar checker fixed? ISTM that it's broken.
One should mot lead astray those who have actually learned proper
English.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 29 '05 #11
Dr John Stockton said the following on 12/29/2005 11:44 AM:
JRS: In article <_L********************@comcast.com>, dated Tue, 27 Dec
2005 21:31:26 local, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
The only reliable way to test for innerHTML support, to date, is to
insert non-normalized HTML into a container using innerHTML and then
read it back. If it is normalized, the browser supports innerHTML. If it
is not normalized, then the browser doesn't support innerHTML.

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.
(1) Is it possible for a browser to support innerHTML as seen from the
scripting side, and yet not to make the corresponding effect on its
visible screen?


Anything is possible so yes it's possible. Is there any way to test for
it other than physically looking at the monitor to see if it changed the
text? No.
(2) Could you get your grammar checker fixed?
There is nothing wrong with my grammar checker.
ISTM that it's broken.
Then you seem to think incorrectly.
One should mot lead astray those who have actually learned proper
English.


Two things:

1)If one is taught "proper English" and gets lead astray by anything I
write then they were not taught properly.

2)Before you get pedantic about grammar and/or spelling perhaps you
should get *your* checkers checked.

mot != not

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

Dec 29 '05 #12

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

Similar topics

16
by: Kerry Neilson | last post by:
For the past couple of months, Idle won't start when I invoke it. I am at a complete loss for why this is. When this happens, they python command line still starts, and python works fine...
1
by: Tom L | last post by:
I have a vb.net app that I created a setup for. I can install it and run it just fine on my XP box, but I'm having problems ont he NT4 workstation box. the nt4 box has sp6a, the .net 1.1 clr,...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
2
by: melanieab | last post by:
Hi, Just checking to see if there's anything I can possibly do to allow for the recognition of the Tab key being pressed. On the KeyDown event I've tried: if (e.KeyCode == Keys.Tab), if...
10
by: PeterW | last post by:
I find in the last 3/4 days that I can't open web applications using VS.net 1.1 for enterprise architiect. The project I am currently working on just refuses to open and hangs. I try using IIS...
5
by: Slavan | last post by:
I have an update statement that I'm executing against Oracle database from my C# code and it won't work. UPDATE MenuCaptions SET Caption = N@Caption WHERE MenuId = @MenuId AND CultureId =...
13
by: Edwin Smith | last post by:
I have a form which displays a DataGridView table generated with the VS2005 tools. The database is a Pervasive v.9 with an ODBC driver. The DataGridView works great except when I'm done and I...
2
tpgames
by: tpgames | last post by:
Two days ago, MS office Word would type JP fonts, I thought. I didn't think I was using Works. Now, it won't type in JP. Jasc Paint shop pro 8, should type JP fonts because I am using XP, according...
2
by: Matthew Wells | last post by:
Hello. I'm reposting this because my prioe post's subject line was incorrect. I'm developing an asp.net 2.0 project using VS 2005 on XP sp2 with all the updates. I have an aspx page with...
2
by: hharry | last post by:
Hello All, Does anyone know of a method to automatically detect if a file is corrupted ? Due to a failed backup process a number of files were corrupted. The files are mostly .xls, .doc, .pdf....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.