Closed Thread Icon

Preserved Topic: Making the script smaller (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18400" title="Pages that link to Preserved Topic: Making the script smaller (Page 1 of 1)" rel="nofollow" >Preserved Topic: Making the script smaller <span class="small">(Page 1 of 1)</span>\

 
Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 04-19-2002 15:00

Hi, I have made a script that calculates some stuff... I just wonder if I can make it smaller in bytes? I heard u could use something with 'eval' to get the field name and then cut the script with 3/4... Am I right?

here's the script:
************************************************
function calcRv(n){
var n;
if(n == 1){
var varAvlast = document.forms["Jobb"].Jobb__Rv1.value;
var varForegaende = document.forms["Jobb"].Maskiner__Rv1_Serv.value;

if(varAvlast == ""){
document.forms["Jobb"].Jobb__Rv1.value = "0";
varAvlast = "0";
};
if(parseInt(varAvlast) >= parseInt(varForegaende))
document.forms["Jobb"].Jobb__Rv1_Kopior.value = (parseInt(varAvlast) - parseInt(varForegaende));
else {
alert("Avlästa värdet är mindre än föregående räkneverk!");
document.forms["Jobb"].Jobb__Rv1_Kopior.value = 0;
document.forms["Jobb"].Jobb__Rv1.focus();
}
}

if(n == 2){
var varAvlast = document.forms["Jobb"].Jobb__Rv2.value;
var varForegaende = document.forms["Jobb"].Maskiner__Rv2_Serv.value;

if(varAvlast == ""){
document.forms["Jobb"].Jobb__Rv2.value = "0";
varAvlast = "0";
};
if(parseInt(varAvlast) >= parseInt(varForegaende))
document.forms["Jobb"].Jobb__Rv2_Kopior.value = (parseInt(varAvlast) - parseInt(varForegaende));
else {
alert("Avlästa värdet är mindre än föregående räkneverk!");
document.forms["Jobb"].Jobb__Rv2_Kopior.value = 0;
document.forms["Jobb"].Jobb__Rv2.focus();
};
}

if(n == 3) {
var varAvlast = document.forms["Jobb"].Jobb__Rv3.value;
var varForegaende = document.forms["Jobb"].Maskiner__Rv3_Serv.value;

if(varAvlast == ""){
document.forms["Jobb"].Jobb__Rv3.value = "0";
varAvlast = "0";
};
if(parseInt(varAvlast) >= parseInt(varForegaende))
document.forms["Jobb"].Jobb__Rv3_Kopior.value = (parseInt(varAvlast) - parseInt(varForegaende));
else {
alert("Avlästa värdet är mindre än föregående räkneverk!");
document.forms["Jobb"].Jobb__Rv3_Kopior.value = 0;
document.forms["Jobb"].Jobb__Rv3.focus();
};
}
if(n == 4) {
var varAvlast = document.forms["Jobb"].Jobb__Rv4.value;
var varForegaende = document.forms["Jobb"].Maskiner__Rv4_Serv.value;

if(varAvlast == ""){
document.forms["Jobb"].Jobb__Rv4.value = "0";
varAvlast = "0";
};
if(parseInt(varAvlast) >= parseInt(varForegaende))
document.forms["Jobb"].Jobb__Rv4_Kopior.value = (parseInt(varAvlast) - parseInt(varForegaende));
else {
alert("Avlästa värdet är mindre än föregående räkneverk!");
document.forms["Jobb"].Jobb__Rv4_Kopior.value = 0;
document.forms["Jobb"].Jobb__Rv4.focus();
}
}
}

*****************************************'


Thanks in advance
/Smash

stinx
Bipolar (III) Inmate

From: London, UK
Insane since: Apr 2002

posted posted 04-19-2002 15:24

I'm guessing the easiest way to cut it down would be to set a variable for all the occurences of
document.forms["Jobb"]

You can set this easily, as js will accept objects as variables - just use
objInp = document.forms["Jobb"];

You can then use
objInp.Jobb__Rv1.value
or
objInp.Jobb__Rv2_Kopior
in place of the longer variable names

stinx

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 04-19-2002 15:47

smash, in addition to what stinx said, you can:

1. define
var varAvlast, varForegaende;
only once in the start of the function
and use this:

varAvlast = parseInt(varAvlast);
varForegaende = parseInt(varForegaende);
and never use parseInt() w/ these variables again,

2.use switch()/case instead of if() statements.


this is the smallest and neatest you can get probably.

Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 04-19-2002 15:53

Thanks for the tips stinx and lallous!

But if I want to use the eval function to get rid of if and switch statements? How would I solve that?

Since I am gonna surf on this page with GPRS which costs per byte I dont want so large javascripts. Instead I want to use eval but I am not sure how. Since the page will decrease in size then...

Thanks In Advance

Smash


lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 04-19-2002 16:16

since you want to save bandwidth try using tools like HTMLCompress ...
basically, whatever browser you're using, enable the caching system and enlarge its buffer, so you save bandwidth through GPRS!

I'm using GPRS too, but It costs me $23 for 120MB a month! $1/MB extra.



mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 04-20-2002 06:37

Using eval() function can only make your script run slower. Also, that function can't replace if statements, only thing that you can optimize is what stinx & lallous said. And in addition to all that you can also remove all whitescape characters (each character = one byte), like spaces, new lines, etc...


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 04-20-2002 09:15

the eval() function isn't necessary. But whoever told you that was on the right track. Your script has four parts that are almost exactly the same:

var varAvlast = document.forms["Jobb"].Jobb__Rv1.value;
var varForegaende = document.forms["Jobb"].Maskiner__Rv1_Serv.value;

if(varAvlast == ""){
document.forms["Jobb"].Jobb__Rv1.value = "0";
varAvlast = "0";
};
if(parseInt(varAvlast) >= parseInt(varForegaende))
document.forms["Jobb"].Jobb__Rv1_Kopior.value = (parseInt(varAvlast) - parseInt(varForegaende));
else {
alert("Avlästa värdet är mindre än föregående räkneverk!");
document.forms["Jobb"].Jobb__Rv1_Kopior.value = 0;
document.forms["Jobb"].Jobb__Rv1.focus();

the only difference is the number after Jobb__RV and Maskiner__Rv. You can replace your four if's with the following:

var varAvlast = document.forms["Jobb"]["Jobb__Rv"+n].value;
var varForegaende = document.forms["Jobb"]["Maskiner__Rv"+n+"_Serv"].value;

if(varAvlast == ""){
document.forms["Jobb"]["Jobb__Rv"+n].value = "0";
varAvlast = "0";
};
if(parseInt(varAvlast) >= parseInt(varForegaende))
document.forms["Jobb"].["Jobb__Rv"+n+"Kopior"].value = (parseInt(varAvlast) - parseInt(varForegaende));
else {
alert("Avlästa värdet är mindre än föregående räkneverk!");
document.forms["Jobb"]["Jobb__Rv"+n+"Kopior"].value = 0;
document.forms["Jobb"]["Jobb__Rv"+n].focus();

Notice that I replaced every variable with a 1 in it with the special way of getting to variables by a string. For instance, I replaced

.Jobb__Rv1

with

["Jobb__Rv"+n]

Whoever told you to use the eval() function was unaware of this method. Eval is often used where this can be used, which makes scripts that take longer to run and are harder to read.

Hope that helps.

Oh, one more thing. All the ["Jobb"]'s that you have in your script can be replaced with just .Jobb - this uses the same principal as above.

Basically,

object.property

is equivalent to

object["property"]

Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 04-22-2002 10:34

Thank you very much for all help!

I really appreciate it!

I've enabled all tips now and they work fine

Thanks again
Smash

« BackwardsOnwards »

Show Forum Drop Down Menu