Tag: js
Javascript ParseInt Vs. ParseFloat
by z3n on May.01, 2010, under Coding, Tips & Hints
Problem:
While debbuging an javascript application i found out a wrong calculation result.
Solution:
Apparently javascript been developed my microsoft, due the extensive issues with many misbehaviors it’s quite sad to find out that there’s no way to escape from them.
var test="08"; alert(parseInt(test)+1); // will return 1 alert(parseFloat(test)+1); // will return 9, as supposed to
Javascript Floating Point Issues
by z3n on Apr.01, 2010, under Coding, Tips & Hints
Problem:
Javascript floating point issues.
Solution:
It’s well known that javascript has major issues when it comes to precision math, even the simplest operations tend to be a nightmare. Although i was aware of that i suddenly found myself into another tricky thing with zeros on the left (eg: 025).
To avoid issues with javascript rounding issues I get all numbers converted to strings, stripping their floating point, this only works if all numbers are from the same precision or if you do a function do that, since there’s no rounding function (besides .toFixed) that does that proprietly.
So, in sum, 1.23 will be calculated as 123, eg:
var a = 1.23; var b = 4.56; a = parseInt(a.toString().replace(/\./g,"")); // 1.23 -> 123 b = parseInt(b.toString().replace(/\./g,"")); // 4.56 -> 4.56 var x = a + b; // = 579 x=x.toString(); return x.substr(0,x.length-2)+"."+x.substr(x.length-2);
Now that’s good, right…?? Apparently yes, but let’s do another example:
var a= 0.25; a = parseInt(a.toString().replace(/\./g,"")); // 0.25 -> 025 var x = 1*a; // 1* 025 = 025 -- right? js will return 021 return x.substr(0,x.length-2)+"."+x.substr(x.length-2);
When there’s a zero on the left, javascript goes nuts. An easy fix would be:
while (x.toString().substr(0,1) == "0")
x=x.substr(1);
and problem solved!
References:
Calling a Javascript function from a different frame
by z3n on Jun.09, 2009, under Coding, Tips & Hints
Problem:
How to call a function from a different frame/iframe ?
Solution:
It depends where the function is, when it’s on the main frame, or if you’re calling a funcion that’s on _top from inside a iframe:
top.function();
If you’re calling a function from a frame to another:
parent.frames['frame_name'].function();
or
parent.frames[frame_number].function();
Javascript to get the whole document width and height
by z3n on May.25, 2009, under Coding, Tips & Hints
Problem:
How to get document’s height and width, not only the view area or the window size, but the whole thing including the scrolling.
Solution:
There are some variants of this solution, because some browsers take this proprieties in a different way, however, here’s a little function i wrote able to return the right dimensions no matter what:
with (document.documentElement) {
var h=Math.max(clientHeight,scrollHeight);
var w=Math.max(clientWidth,scrollWidth);
}
there’s also the inner values, but they only work on certain browsers, this would be able to get the numbers you need on IE6+ , FF and mostly of the decent browsers, if you must have working on every browser check quirksmode. This will also return the highest value, meaning that if the page itself won’t fill client’s width/height, it will get the biggest value, perfect when you need to do a full page div or something like that.
Sources:
Simple AJAX/xmlhttp query issue
by z3n on May.20, 2009, under Coding, Tips & Hints
Problem:
You like jQuery, however, doing simple tasks get impossible since jQuery is too massive, even packed it’s over 50kb. The alternate, script.taculo.us, is even bigger!
Solution:
Doing a simple ajax or xmlhttp request, is nothing out of this world, actually, it’s pretty easy, you just need to watch it if you do more than one on the same page, or when a error happen you might want to have a handler.
Based on quirks mode example i wrote this little function, it’s able to do a rudimentar queue and return plain text from a remote server, it’s also compatible with IE.
-
_lD=function(url){
-
if(p==1){
-
setTimeout("_lD('"+url+"')",250);
-
}else{
-
p=1;
-
x=null;
-
if(window.XMLHttpRequest){
-
x=new XMLHttpRequest();
-
}else if(window.ActiveXObject){
-
x=new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
if(x!=null){
-
x.onreadystatechange=function(){
-
var r="";
-
if(x.readyState==4){
-
if(x.status==200){
-
r=x.responseText;
-
-
// manipulate your result here
-
-
p=0;
-
}
-
}
-
}
-
x.open("GET",url,true);
-
x.send(null);
-
}
-
}
-
}
Packed, this function is less than 500 bytes, much better than the 58kb of jQuery, heheh
JS and CSS Packer in PHP
by z3n on Apr.03, 2009, under Coding
Problem:
No decent css/js packers in php
Solution:
This CLI based script will compress the input files detecting their formats and placing on a defined folder, easying the automation of multiple files compression.
have fun!