You are viewing our Forum Archives. To view or take place in current topics click here.
[JS] Extremely basic javascript question about shortcuts?
Posted:

[JS] Extremely basic javascript question about shortcuts?Posted:

7en
  • Wise One
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
I know from experience as you gain skill in a coding language, you learn all the tricks + shortcuts to cut down in the size of your file and make it neater

Any tips to make this smaller? It's basically the same thing copypasted out 5 times, im pretty new to JS (obviously), and am probably unaware of some nifty trick All help greatly appreciated!

EDIT- Wow nevermind, TTG won't let me post the code..? Some error about the html tags..

Here is a screencap:

[ Register or Signin to view external links. ]
#2. Posted:
PureCyanideKiss
  • Resident Elite
Status: Offline
Joined: Jan 05, 200915Year Member
Posts: 289
Reputation Power: 16
Status: Offline
Joined: Jan 05, 200915Year Member
Posts: 289
Reputation Power: 16
It's saying there is no image.
#3. Posted:
7en
  • Wise One
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
PureCyanideKiss wrote It's saying there is no image.


Loads fine for me buddy
#4. Posted:
DimiPro
  • TTG Senior
Status: Offline
Joined: Oct 18, 201112Year Member
Posts: 1,200
Reputation Power: 48
Status: Offline
Joined: Oct 18, 201112Year Member
Posts: 1,200
Reputation Power: 48
if you want to make it look neater you can use notepad ++ you can minimize all functions , forms , html

heres some examples :

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]
#5. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
TTG_Jok3rs_Lobbies wrote if you want to make it look neater you can use notepad ++ you can minimize all functions , forms , html

heres some examples :

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

I don't quite think that's what he meant..


If the size of the file is your concern and rendering etc you can always use js compression.
Luckily for you there's plenty of online, javascript compressors such as:
[ Register or Signin to view external links. ]
Also I believe even google have a product of which does the same.
Anyway I don't know if this is answering you question or not but, the size of the file will drop considerably after compression.

As far as "tidiness" is concerned it really comes down to personal preferences, if you don't share your code it shouldn't matter what it looks like as long as you understand it.


edit* Though you can put separate functions inside one script tag, it may look neater:

[script]
function TurnBlue(){
//something
}
function TurnRed(){
//something
}
[/script]

yea, I can't post html tags either but, you see what I mean.


Last edited by iyop45 ; edited 3 times in total
#6. Posted:
DimiPro
  • TTG Senior
Status: Offline
Joined: Oct 18, 201112Year Member
Posts: 1,200
Reputation Power: 48
Status: Offline
Joined: Oct 18, 201112Year Member
Posts: 1,200
Reputation Power: 48
iyop45 wrote
TTG_Jok3rs_Lobbies wrote if you want to make it look neater you can use notepad ++ you can minimize all functions , forms , html

heres some examples :

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

I don't quite think that's what he meant..


If the size of the file is your concern and rendering etc you can always use js compression: Luckily for you there's plenty of online javascripy compressors such as [ Register or Signin to view external links. ]
Also I believe even google have a product of which does the same.
Anyway I don't know if this is answering you question or not but, the size of the file will drop considerably after compression.

As far as "tidyness" is concerned it really comes down to personal preferences, if you don't share your code it shouldn't matter what it looks like as long as you understand it.


edit* You can put all the seperate functions inside one script tag by the way, it make look neater:

[script]
function TurnBlue(){
//something
}
function TurnRed(){
//something
}
[/script]

yea, I can't post html tags either but, you see what I mean.


you could save it as.html and then compress the file
#7. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
TTG_Jok3rs_Lobbies wrote
iyop45 wrote
TTG_Jok3rs_Lobbies wrote if you want to make it look neater you can use notepad ++ you can minimize all functions , forms , html

heres some examples :

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

I don't quite think that's what he meant..


If the size of the file is your concern and rendering etc you can always use js compression: Luckily for you there's plenty of online javascripy compressors such as [ Register or Signin to view external links. ]
Also I believe even google have a product of which does the same.
Anyway I don't know if this is answering you question or not but, the size of the file will drop considerably after compression.

As far as "tidyness" is concerned it really comes down to personal preferences, if you don't share your code it shouldn't matter what it looks like as long as you understand it.


edit* You can put all the seperate functions inside one script tag by the way, it make look neater:

[script]
function TurnBlue(){
//something
}
function TurnRed(){
//something
}
[/script]

yea, I can't post html tags either but, you see what I mean.


you could save it as.html and then compress the file


But, you can't use the compressed file in that state, so you would still have to decompress the file in order for the server to use it. I'm referring to js compression of which is compressing the javascript code within the file rather than compressing the file as whole. So it still functions the same but, takes up less disk space.

*Js compression also acts as a good obfuscator.
#8. Posted:
Imp
  • Retired Staff
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Abbreviate wrote I know from experience as you gain skill in a coding language, you learn all the tricks + shortcuts to cut down in the size of your file and make it neater

Any tips to make this smaller? It's basically the same thing copypasted out 5 times, im pretty new to JS (obviously), and am probably unaware of some nifty trick All help greatly appreciated!

EDIT- Wow nevermind, TTG won't let me post the code..? Some error about the html tags..

Here is a screencap:

[ Register or Signin to view external links. ]


This is personal opinion, but when I code, I try to think, what job is each function doing? If it is the same job, then think of a way to make 1 function to do the job, rather than 5 functions ;)

Here is my example.


/ img id="blue" onClick="ChangeColour(this.id)" src="blue.png"/
/ img id="red" onClick="ChangeColour(this.id)" src="red.png"/
/ img id="yellow" onClick="ChangeColour(this.id)" src="yellow.png"/
/ img id="green" onClick="ChangeColour(this.id)" src="green.png"/
/ img id="black" onClick="ChangeColour(this.id)" src="black.png"/

/ script/
function ChangeColour(clicked_id)
{
    element=document.getElementById('LargeImage');
    element.src=clicked_id + ".png";
}
/ /script/



(I have enclosed my html tags with // rather than <> )


I havent tested this, but this basically uses the id of the Image you have clicked to build the image string. Therefore only 1 function for all 5 colours
#9. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
iyop45 wrote
TTG_Jok3rs_Lobbies wrote if you want to make it look neater you can use notepad ++ you can minimize all functions , forms , html

heres some examples :

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

[ Register or Signin to view external links. ]

I don't quite think that's what he meant..


If the size of the file is your concern and rendering etc you can always use js compression.
Luckily for you there's plenty of online, javascript compressors such as:
[ Register or Signin to view external links. ]
Also I believe even google have a product of which does the same.
Anyway I don't know if this is answering you question or not but, the size of the file will drop considerably after compression.

As far as "tidyness" is concerned it really comes down to personal preferences, if you don't share your code it shouldn't matter what it looks like as long as you understand it.


edit* Though you can seperate functions inside one script tag, it may look neater:

[script]
function TurnBlue(){
//something
}
function TurnRed(){
//something
}
[/script]

yea, I can't post html tags either but, you see what I mean.


Exactly what I needed, +rep. Thanks to everyone else that tried to help
#10. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Ah np, I was wondering whether I answered your question or not.

Glad I could help
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.