friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2007-06-14 08:42:08

marfillaster
» Promoter
marfillaster's display avatar
Friendster Noob ‹
Class X
Location: azeroth
Registered: 2006-11-10
Posts: 1017
Last visit: 2008-11-27
Reputation: 48
Friendster

javaScript Functions explained

Oh well, i made this topic because i've seen alot of codes here being misused.

So what is a function?

functions is a block of code that perform a routine or process. it can return a value or not return anything at all, just execute a block of code. It can also accept arguments or parameters. A function must not be duplicated in a document(which may contain more that 1 javascript). However it can be called many times.

Here's some example of the syntax, foo can be anything provided it doesnt begin with number and only contains letter and underscore.

function that execute a code

function foo() {
alert("hi");
}

how to call:

foo();

will show an alert with "hi" message

function that execute a code but requires a parameter

function foo(message) {
alert(message);
}

how to call:

foo("hi");

will show an alert with "hi" message

function returns a value

function foo() {
return "hi";
}

how to call:

alert(foo());

will show an alert with "hi" message

function returns a value but requires a parameter

function foo(message) {
if(message) return true;
return false;
}

how to call:

alert(foo("hi"));

will show an alert with "true" message


so now you know how to use a function. As I said a function must not be duplicated, it will cause bugs if you do.

Why use functions?
Basically we want our code short, in practicality we use function so that we can execute the same code many times without making the same code.

What tricks here in ftalk do use functions?
All of my codes uses function. That includes addSideBar, addMainbar and wvm.

Ok here's an example how we will apply the purpose of a function, we will use addSideBar.

Here's the addSideBar function,

Code:

function addSideBar(head,htm,div_id) {

    var innerHtm=htm;
    var cont=
       
        "<div id='"+div_id+"' class='commonbox "+div_id+"'>"+
        "<h2>"+head+"</h2>"+
"<div id='content_"+div_id+"'>"+
        innerHtm+
"</div>"+
       
        "</div>";

    try {
        var obj=document.createElement("<li>");
    } catch(e) {
        var obj=document.createElement("li");
    }
    var x=document.getElementById("2");
    x.parentNode.parentNode.appendChild(obj);
    obj.innerHTML=cont;
}

Say we want to put three sidebars in our profile, each one will have an image link. Again, we only need a single block of addSideBar function for the three sidebars.

if (!attachOnLoadHandler(function(){editContent()})) parent.onload = function(){editContent()};
function editContent() {
  var html1="<a href=\"somelink1.html\"><img  src="someimage1.bmp" /></a>";
  addSideBar("Title 1",html1,"sidebar1");
  var html2="<a href=\"somelink2.html\"><img  src="someimage2.bmp" /></a>";
  addSideBar("Title 2",html2,"sidebar2");
  var html3="<a href=\"somelink3.html\"><img  src="someimage3.bmp" /></a>";
  addSideBar("Title 3",html3,"sidebar3");
}



function addSideBar(head,htm,div_id) {

    var innerHtm=htm;
    var cont=
       
        "<div id='"+div_id+"' class='commonbox "+div_id+"'>"+
        "<h2>"+head+"</h2>"+
"<div id='content_"+div_id+"'>"+
        innerHtm+
"</div>"+
       
        "</div>";

    try {
        var obj=document.createElement("<li>");
    } catch(e) {
        var obj=document.createElement("li");
    }
    var x=document.getElementById("friends_2_2");
    x.parentNode.parentNode.appendChild(obj);
    obj.innerHTML=cont;
}

wait a minute, where the hell did that attachOnLoadHandler come from?
attachOnLoadHandler is also a function, one of friendster's js(http://images.friendster.com/200706A/js…ster_v1.js) already does have this function so we do no longer need to copy the whole attachOnloadhandler code. in the code above, we only called it and passed a parameter which is also a function(editcontent). So this demonstrates why functions must not be duplicated.

I hope you get my point...

Last edited by Ephemeral (2008-03-04 13:36:27)

#2  2007-06-14 08:57:00

eykalsyamim
» Retired Moderator
eykalsyamim's display avatar
On holiday!! Two months..
Class-S
Location: Malaysia
Registered: 2007-01-24
Posts: 5306
Reputation: 13
Friendster

Re: javaScript Functions explained

this is gud..lecture time..  :thumbsup:  :thumbsup:
newbie must undstnd this 1st..  :thumbsup:

Last edited by eykalsyamim (2007-06-14 08:58:59)


I'm a newbie..

Compiling javascripts in one file Click Here
Complete Compilation of CSS Codes! Click Here

#3  2007-06-14 08:58:07

feruzz
» Retired Moderator
feruzz's display avatar
X-Mod
Class-S
Location: Malaysia
Registered: 2007-02-18
Posts: 8262
Reputation: 134
???

Re: javaScript Functions explained

yeah..I've learn something new... =)  :thumbsup:  :thumbsup:

#4  2007-06-14 09:01:30

marfillaster
» Promoter
marfillaster's display avatar
Friendster Noob ‹
Class X
Location: azeroth
Registered: 2006-11-10
Posts: 1017
Last visit: 2008-11-27
Reputation: 48
Friendster

Re: javaScript Functions explained

i hope this will help all of you guys

#5  2007-06-14 09:06:51

feruzz
» Retired Moderator
feruzz's display avatar
X-Mod
Class-S
Location: Malaysia
Registered: 2007-02-18
Posts: 8262
Reputation: 134
???

Re: javaScript Functions explained

yeah...many newbies didn't understand a function...they just copied & paste :wasted:
so I hope this tutorial would help them =)
keep it up Ken :thumbsup:

#6  2007-06-14 09:09:14

eykalsyamim
» Retired Moderator
eykalsyamim's display avatar
On holiday!! Two months..
Class-S
Location: Malaysia
Registered: 2007-01-24
Posts: 5306
Reputation: 13
Friendster

Re: javaScript Functions explained

yup..like me..i'm juz a newbie..didnt know anythg..juz copy and paste.hehe..
gud job..  :thumbsup:  :thumbsup:


I'm a newbie..

Compiling javascripts in one file Click Here
Complete Compilation of CSS Codes! Click Here

#7  2007-06-14 09:10:39

feruzz
» Retired Moderator
feruzz's display avatar
X-Mod
Class-S
Location: Malaysia
Registered: 2007-02-18
Posts: 8262
Reputation: 134
???

Re: javaScript Functions explained

eykalsyamim wrote:

yup..like me..i'm juz a newbie..didnt know anythg..juz copy and paste.hehe..
gud job..  :thumbsup:  :thumbsup:

like you?? :o  :o  :o  :o

#8  2007-06-14 11:04:56

leftalone
» FriendsterGeek
Registered: 2007-04-13
Posts: 1457
Last visit: 2008-10-26
Reputation: 4

Re: javaScript Functions explained

haha tanks for sharing sir ken :)

#9  2007-06-14 11:14:41

axce.lan
» FriendsterNewbie
Location: Kuala Lumpur,MY
Registered: 2007-06-03
Posts: 16
Last visit: 2007-12-17
Reputation: ~
Friendster

Re: javaScript Functions explained

nice info...even myself dunno how it works... :lol: :P


Signature Removed.
10. Signatures. Don't use more than one banner. Don't make it excessive and annoying. Maximum image height for signature is 200px. No audio or video allowed. We have the right to edit or remove objectionable signature.

Have a nice day: Ephemeral

#10  2007-06-14 11:42:31

- razian -
» FriendsterAddict
- razian -'s display avatar
BUMP XD
Location: no-where
Registered: 2007-01-15
Posts: 427
Last visit: 2008-11-28
Reputation: 24
Friendster

Re: javaScript Functions explained

thanks for info
thanks 4 sharing too  :D


Blessthefall Underoath From First To Last Saosin Thursday Silverstein The Used The Devil Wears Prada The Word Alive Alesana Attack Attack! Before Their Eyes Bring Me The Horizon  Burden Of A Day Chiodos Drop Dead Gorgeous Emanuel Emarosa Escape The Fate Hawthorne Heights Hopes Die Last I Am Ghost LoveHateHero A Skylit Drive Finch Pierce The Veil

#11  2007-06-14 11:46:58

ric25
» FriendsterManiac
ric25's display avatar
Location: planet earth
Registered: 2007-04-13
Posts: 751
Reputation: 12

Re: javaScript Functions explained

thanks for the info.. ;)  :thumbsup:


#12  2007-06-14 13:17:31

jagcopra
» FriendsterAddict
jagcopra's display avatar
Let me guess??
Location: Where I belong..
Registered: 2007-03-14
Posts: 572
Last visit: 2008-11-13
Reputation: 4

Re: javaScript Functions explained

hehe.. thx for the tutorial.. simple yet very useful!!.. you can use this simple tutorial to a complicated and more great scripts!!.. =)


Get Firefox now!
Features:
>Tabbed browsing make reading webpages easier.
>It's more secure against viruses and spyware
>It keeps Microsoft from controlling the future of the internet
>It's better for web designers and developers

#13  2007-06-14 17:38:26

TA Juleigtin Siahaan
» Moderator
TA Juleigtin Siahaan's display avatar
«•ºAyano Shiratoriº•»
Class X
Location: C:\^Serayu Valley^
Registered: 2007-01-12
Posts: 4594
Reputation: 146
Friendster

Re: javaScript Functions explained

wew.. :D  :D

Waahhh..Finally you posted this topic.. :thumbsup:  :thumbsup:

I think this topic will be useful.. =)  =)

Thanks 4 sharing it Mr.Marfillaster.. :eh:  :)

#14  2007-06-14 17:44:06

xavierkym
» Retired Moderator
The Sinister
Class-S
Location: London, England
Registered: 2007-06-10
Posts: 5370
Reputation: 129
Friendster

Re: javaScript Functions explained

wow... a great help 4 everyone.. weeeee...  :D


#15  2007-06-15 13:57:46

joe1084
» FriendsterNewbie
Location: indonesia
Registered: 2007-05-24
Posts: 63
Last visit: 2008-11-18
Reputation: ~
Friendster

Re: javaScript Functions explained

i got headache  :wallbash:


haryokosda@yahoo.com
www.friendster.com/40408374

#16  2007-06-15 13:58:52

marfillaster
» Promoter
marfillaster's display avatar
Friendster Noob ‹
Class X
Location: azeroth
Registered: 2006-11-10
Posts: 1017
Last visit: 2008-11-27
Reputation: 48
Friendster

Re: javaScript Functions explained

why?

#17  2007-06-15 14:11:16

cip6192
» Promoter
cip6192's display avatar
[ Plaifer Cipher ]
Class-S
Location: Nagareyama, Chiba
Registered: 2007-05-13
Posts: 1553
Last visit: 2008-08-06
Reputation: 7
Friendster

Re: javaScript Functions explained

wow marfilla thanks for sharing that haha i already knew that but it did a lot of helps for those who asking how to put mp3's, videos, cbox, WVM etc.  they can really do it in one js file only right?  :D  comment me if im wrong  :P


#18  2007-06-15 14:19:12

marfillaster
» Promoter
marfillaster's display avatar
Friendster Noob ‹
Class X
Location: azeroth
Registered: 2006-11-10
Posts: 1017
Last visit: 2008-11-27
Reputation: 48
Friendster

Re: javaScript Functions explained

yeah, only one file, and much smaller...

#19  2007-06-29 16:38:33

joez
» FriendsterTalker
joez's display avatar
->virtouso....
Location: Hiding.(im wanted)
Registered: 2007-06-22
Posts: 198
Last visit: 2008-11-26
Reputation: 1
Friendster

Re: javaScript Functions explained

it grinds my brain....  =D  =D


#20  2007-07-05 07:47:04

diabolicious
» FriendsterGeek
diabolicious's display avatar
EPIC
Location: South East Asia
Registered: 2007-06-23
Posts: 1299
Last visit: 2008-11-05
Reputation: 21
Friendster

Re: javaScript Functions explained

:o now i understand it a bit
<~~~ err im still cursed :crybaby:


Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2008 PunBB

[ 30 queries - 0.855 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)