JavaScript Confusions

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 3rd, 2010, 12:47 am

While you're on the subject of parseInt, it's always nice to know you can pass parseInt a second radix argument.

The easiest way I can describe how it works is to use hexadecimal numbers. Say for instance you have the following hex string.

Code: [ Select ]
FF


Looks a lot like something you'ld use for a CSS color value, right ?

Anywho, if you pass 16 as the second argument to parseInt, it will tell you the real number that hex string translates to.

Code: [ Select ]
var N = parseInt('ff', 16); // N = 255
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 3rd, 2010, 12:47 am

  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 3rd, 2010, 6:10 pm

Thanks for that explanation. I don't know if you used documentation for that or not, and if you didn't, you certainly know JavaScript very well.

I read that in my JavaScript book, so I had an idea about it. But anything that would help me learn JavaScript is appreciated. Thanks.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 3rd, 2010, 6:21 pm

I have one question.

I have the following code:
JAVASCRIPT Code: [ Select ]
onload = function()
{
    newGame();
}
 
function newGame()
{
    var cash = document.getElementById('cash_owned');
   
    cash.innerHTML = '200';
}
  1. onload = function()
  2. {
  3.     newGame();
  4. }
  5.  
  6. function newGame()
  7. {
  8.     var cash = document.getElementById('cash_owned');
  9.    
  10.     cash.innerHTML = '200';
  11. }
But that doesn't do anything.

Any help? I tried moving the function newGame(); in front of that onload function, but it didn't work anyway.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 3rd, 2010, 10:22 pm

Does your browsers error console mention any javascript errors ?
Strong with this one, the sudo is.
  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jun 09, 2009
  • Posts: 334
  • Status: Offline

Post February 4th, 2010, 1:28 pm

joebert wrote:
Does your browsers error console mention any javascript errors ?


Yo, you definitely want to be using the "firebug" plugin for firefox when programming js.

Bogey wrote:
JAVASCRIPT Code: [ Select ]
onload = function()
 
  1. onload = function()
  2.  
But that doesn't do anything.


You probably have to use document.body.onload or window.onload, presuming your script is loaded in <head> which is the only way this will work. The way I would normally do this is to use the <body> tag:
Code: [ Select ]
<body onload="newGame()">


Although more often I just include an initialization script at the end of the page:
Code: [ Select ]
<script type="text/javascript">
[set some variables]
init(); /* from script loaded in the head */
</script>
</body>
  1. <script type="text/javascript">
  2. [set some variables]
  3. init(); /* from script loaded in the head */
  4. </script>
  5. </body>

This way, if you want assign some attributes (et al.) in your js script you can do that in the init() function, and be sure all your elements actually exist regardless of browser.
Image
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 4th, 2010, 9:05 pm

Neither of those works, and while the onload as the body attribute works, it is not valid XHTML. Maybe I shouldn't worry about that while learning JavaScript... I'm just trying to figure out how to make that work.

@Joebert: No. I'm probably going to get firebug.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jun 09, 2009
  • Posts: 334
  • Status: Offline

Post February 5th, 2010, 5:08 pm

Bogey wrote:
the onload as the body attribute works, it is not valid XHTML.


Ah. The second option will pass xhtml strict, and is in fact is the most robust/foolproof & useful. Of course, then you may p-off some people who are very anal about not including too much javascript in the page source. :lol: Which only matters if such people matter in your situation (I guess it all depends how much you want to contort yourself).
Image

Post Information

  • Total Posts in this topic: 22 posts
  • Users browsing this forum: No registered users and 189 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.