Simple Machine Codes
March 21, 2010, 04:09:37 pm
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to SMC, the best place for your SMF For Free Codes
 
  Home Help Search Arcade Affiliates Code Index Staff List Calendar Members Login Register  

Tutorial #1: Starting Out


Pages: [1]   Go Down
  Print  
Author Topic: Tutorial #1: Starting Out  (Read 621 times)
0 Members and 1 Guest are viewing this topic.
Agent Moose
Administrator

Offline Offline

Gender: Male
Posts: 2,668



View Profile WWW
« on: December 29, 2007, 10:13:32 am »

As you should have read by now, Javascript is always contained within <script> tags on the page. There's no quick and easy php way of doing <? to get around it, you're going to have to start your script with either <script> or <script type="text/javascript">, and end it with </script>. You're not allowed to have any </script> or <script>'s inside your code, so you're only declaring it's a script once.

Good Coding
Code:
<script type="text/javascript">
if(DoSomething){
something();
};
</script>

Bad Coding
Code:
if(DoSomething){
<script type="text/javascript">
something();
</script>

Worse Coding
Code:
<script type="text/javascript">
if(DoSomething){
<script type="text/javascript">
something();
</script>
};
</script>

The first Bad Coding will run, it will not do the if statement, but it will not error, since everything in the script if proper Javascript. The Worse Coding will see the second <script> tag and just exit out, giving you an error in your browser.

This leads to the error consoles that are on your browsers.  All browsers have them.  It lets you check the errors you have on the page your viewing.

With Internet Explorer you will get a a message at the bottom left corner of your browser, saying that there are errors on the page.

With Fire Fox, you can go to Tools >> Error Console.

If you double click on the IE icon, you'll get up a message box, and you can cycle through the errors on the page. The FireFox dialog generally gives you more (and better) information about the error you're receiving, and you can also click the error to see the line that it's erroring out on. Very useful.

Now, say we have this:
Code:
<script type="text/javascript">
someError();
</script>

All we did was call a function to start, but if that is all you have, you will get an error saying that someError is not a function.  So, to make the function, you would do this:

Code:
<script type="text/javascript">
function someError(){
//Do something here
};
</script>

More on Functions later.

Last thing for this tutorial, a bit of javascript etiquette when you're writing.

Code:
<script type="text/javascript">
if(Statement){
doSomething();
}else{
doSomethingElse();
}
</script>

gives exactly the same output as:

Code:
<script type="text/javascript">if(Statement) doSomething() else doSomethingElse </script>

But I know which one i'd rather read. Break up your code on new lines, your code should have the if statement on one line, with the { at the end of it. The else statement should be on one line with a } before it and { after, and the final end of the if statement on its own line. You don't always need an else, but put the last } on it's own line. It's just easier to read, and for the purposes of this tutoring group, it's the style we're going to be using!
« Last Edit: February 04, 2008, 04:19:12 am by Omikron 9861 » Share Report Spam   Logged

Myrtle
The Girl With The Forum.
100 Club Member

Offline Offline

Gender: Female
Posts: 480


Death Note(Credits to Kai): Myrtle_Gail, Myr-chan


View Profile WWW
« Reply #1 on: February 25, 2008, 02:14:22 am »

Thanks Moose, I'm starting out in codes and I feel like a total dufus at it. But I'm trying hard to get it.   Afro

Great Tutorial. I'm trying to read them all.
Report Spam   Logged

Blahs
100 Club Member

Offline Offline

Posts: 184


Rockstar!


View Profile WWW
« Reply #2 on: March 17, 2008, 05:58:17 am »

Nice guide but correct me if im wron this code:

Code:
<script type="text/javascript">
if(Statement){
doSomething();
}else{
doSomethingElse();
}
</script>

Shouldn't it be like this:

Code:
<script type="text/javascript">
if(Statement){
doSomething();
}else
doSomethingElse();
}
</script>
Report Spam   Logged


Join my forums!!!!
Url: http://silabforums.smfforfree4.com/index.php
Agent Moose
Administrator

Offline Offline

Gender: Male
Posts: 2,668



View Profile WWW
« Reply #3 on: March 17, 2008, 07:52:51 am »

Correct Smiley  It will not work since your missing a { in there Smiley
Report Spam   Logged

Blahs
100 Club Member

Offline Offline

Posts: 184


Rockstar!


View Profile WWW
« Reply #4 on: March 17, 2008, 02:28:14 pm »

Correct Smiley  It will not work since your missing a { in there Smiley
really I think that it won't be needed i guess im wrong hmmm...
Report Spam   Logged


Join my forums!!!!
Url: http://silabforums.smfforfree4.com/index.php
Agent Moose
Administrator

Offline Offline

Gender: Male
Posts: 2,668



View Profile WWW
« Reply #5 on: March 17, 2008, 05:28:29 pm »

You wont need it if you do this:
Code:
<script type="text/javascript">
if(Statement) doSomething() else doSomethingElse();
</script>

Just other Than that, you need it.  The brackets are what is holding the information in the if...else statement.
Report Spam   Logged

legend 1234
100 Club Member

Offline Offline

Gender: Male
Posts: 461


Legend 1234


View Profile WWW
« Reply #6 on: April 12, 2008, 08:18:25 pm »

oo sweet mosey i just saw this then l0ol

nice mate

legend 1234
Report Spam   Logged

Agent Moose
Administrator

Offline Offline

Gender: Male
Posts: 2,668



View Profile WWW
« Reply #7 on: April 13, 2008, 09:30:30 pm »

Glad you like it Smiley
Report Spam   Logged

robo123
Starter

Offline Offline

Posts: 24


View Profile
« Reply #8 on: May 07, 2008, 11:17:31 am »

thanks nice tutorial.
Report Spam   Logged
Kalphiter
100 Club Member

Offline Offline

Gender: Male
Posts: 203


Never forget to drink coffee.


View Profile
« Reply #9 on: June 18, 2008, 09:43:02 pm »

You wont need it if you do this:
Code:
<script type="text/javascript">
if(Statement) doSomething() else doSomethingElse();
</script>

Just other Than that, you need it.  The brackets are what is holding the information in the if...else statement.
I could never explain brackets, I just thought starting/closing!
Report Spam   Logged

Java is your friend.
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2006-2009, Simple Machines LLC
Hostgator Hosting