LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-30-2009, 02:19 PM   #1
SangrelX
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Rep: Reputation: 0
JavaScript IF ELSE not functioning correctly! HELP


Ok guys this makes no sense to me so im hoping someone can spot the error


java code snipplet below

Code:
<script type="text/JavaScript">
addEvent(window, 'load', initCorners);
function initCorners() {
var setting = {
tl: { radius: 6 },
tr: { radius: 6 },
bl: { radius: 6 },
br: { radius: 6 },
antiAlias: true
}

var rbar = documentgetElementByID('r_sidebar')
var lbar = documentgetElementByID('l_sidebar')
var wrapped = documentgetElementByID('wrap')
var cleft = documentgetElementByID('contentleft')

if (rbar != r_sidebar) { 
}else{
curvyCorners(setting, ".r_sidebar");
}

if (lbar != l_sidebar) { 
}else{
curvyCorners(setting, ".l_sidebar");
}

if (wrapper != wrap) { 
}else{
curvyCorners(setting, ".wrap");
}

if (cleft != contentleft) { 
}else{
curvyCorners(setting, ".contentleft");
}



curvyCorners(setting, ".roundedCorners");
curvyCorners(setting, ".hrr");
curvyCorners(setting, ".content");
}
</script>

as you can see im trying to verify a Element ID Exists - if it doesnt Exist DO NOTHING if it DOES Exist then set the option for the function

Its not working properly no matter what i try it still sets the option and the function throws an error any page that the Element doesnt exist on?

any ideas?
 
Old 08-30-2009, 02:28 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. Semicolons are good:
Code:
var rbar = documentgetElementByID('r_sidebar');
2. The test is too complex:
Code:
  // Poor
  if (rbar != r_sidebar) { 
  }else{
  curvyCorners(setting, ".r_sidebar");
  }

  // Better
  if (rbar == r_sidebar)
    curvyCorners(setting, ".r_sidebar");
3. You're not correctly testing for (the very real case!) "object not found":
Code:
  if ( (rbar) && (rbar == r_sidebar) )
    curvyCorners(setting, ".r_sidebar");
'Hope that helps .. PSM

PS:
Strong recommendation:
Install Firefox (if you haven't already) and install the Firebug debugger. It's totally indispensable!
 
Old 08-30-2009, 05:05 PM   #3
SangrelX
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
Hi -

1. Semicolons are good:
Code:
var rbar = documentgetElementByID('r_sidebar');
2. The test is too complex:
Code:
  // Poor
  if (rbar != r_sidebar) { 
  }else{
  curvyCorners(setting, ".r_sidebar");
  }

  // Better
  if (rbar == r_sidebar)
    curvyCorners(setting, ".r_sidebar");
3. You're not correctly testing for (the very real case!) "object not found":
Code:
  if ( (rbar) && (rbar == r_sidebar) )
    curvyCorners(setting, ".r_sidebar");
'Hope that helps .. PSM

PS:
Strong recommendation:
Install Firefox (if you haven't already) and install the Firebug debugger. It's totally indispensable!


Paul-

I actually have tested it with semicolons - I forgot to put em back when I tested the method above again LOL..

I do alot of PHP coding and javascript is similar to it but it seems there are some differences I just have not learned yet

Im trying to expand my Javascript skills a bit lol


I had tried all the methods you showed me except the last one the

" testing for (the very real case!) "object not found":"

so ill give that a whirl.

thanks for the input

p.s. I have FireFox Just dont use it often and forgot totally about FireBug!!! (smacks self in head)


did not work !!! arg.

ok let me further explain this

I have say 2 pages

page 1 contains all the div ids that are listed in the javascript function for roundcorners

page 2 DOES NOT have division id contentleft, r_sidebar, l_sidebar, blah blah

HOW can I tell the javascript to CHECK if the div id exists BEFORE assign ing the settings to the roundcorners function?

because as it is now whenever a user travels to a page that does not contain the divisions it throws an error alert up! and well I cant have that lol

Last edited by SangrelX; 08-30-2009 at 05:12 PM.
 
Old 08-30-2009, 08:12 PM   #4
headrift
Member
 
Registered: Sep 2005
Distribution: Gentoo, Sabayon, Puppy, Arch
Posts: 165

Rep: Reputation: 29
Quote:
Originally Posted by SangrelX View Post
HOW can I tell the javascript to CHECK if the div id exists BEFORE assign ing the settings to the roundcorners function?
Done a try/catch block? It sounds like what you need. Something along these lines:

Code:
for(divId in divIdList){
  try{
    divId = document.getElementById(divId);
  }catch(ex){
    continue;
  }

  //  Do your stuff here
}
If devId doesn't pull an object, an error will be thrown and the loop will continue. Otherwise, the corner stuff is free to run.

...should work, but is kind of ugly. I don't like using exceptions to handle "normal" runtime situations. I personally would find a way to keep track of what ids are used in which pages. Much slicker.
 
Old 08-30-2009, 09:55 PM   #5
SangrelX
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by headrift View Post
Done a try/catch block? It sounds like what you need. Something along these lines:

Code:
for(divId in divIdList){
  try{
    divId = document.getElementById(divId);
  }catch(ex){
    continue;
  }

  //  Do your stuff here
}
If devId doesn't pull an object, an error will be thrown and the loop will continue. Otherwise, the corner stuff is free to run.

...should work, but is kind of ugly. I don't like using exceptions to handle "normal" runtime situations. I personally would find a way to keep track of what ids are used in which pages. Much slicker.

lol guys sorry to have wasted your time!!

I found a solution the RoundCorners script has a built in function that blocks alert messages for missing divs!

I scanned over their faq the other night and didnt see this tidbit so again I apologize for wasting your time

Thanks
SangrelX
 
  


Reply

Tags
else, help, if, java, script, statement


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sound not functioning correctly in new Slackware 12.1 install wizardhat Slackware 2 11-30-2008 07:10 AM
CD Burner not functioning correctly after upgrade jonlake Linux - Hardware 2 11-17-2006 10:29 AM
PHP not functioning correctly dannylee Programming 7 08-30-2006 05:25 AM
How do I test that my dns server is functioning correctly adam_ant Linux - Networking 4 12-13-2005 02:31 PM
USB Not Functioning Correctly. Phoenix_Zero Linux - Newbie 3 01-08-2005 01:42 PM


All times are GMT -5. The time now is 05:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration