Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Sunday, January 6, 2013

Problem to parse int in JavaScript

we face problem when try to:
parseInt('08') returns 0

The leading zero in the string tells the Javascript engine that it is an octal number. Because 8 and 9 are not valid numbers in octal, parseInt returns 0. This is expected behaviour because they are not valid octal integers and parseInt returns 0 because the first valid number encountered is a zero.
The solution

After a quick bit of research when I had this problem, I discovered the parseInt function has an optional 'radix' parameter which specifies the base to use. By passing 10 as the base it solves the leading 0 issue, for example:
 alert( parseInt('08', 10) );

will show the number 8 in an alert dialog, whereas:
 alert( parseInt('08') );

would display 0.
Conclusion

To be on the safe side it's probably always a good idea to pass the radix parameter to the parseInt function to ensure the values returned are what you expect them to be, especially if they might contain leading zeroes.

Wednesday, March 21, 2012

Line limit for Text Area

In a HTML page, text area is used to accept multiple line texts at once. But some times, we may need to fixed the no. of line in a text area of web page. There is not attribute available for this purpose. This will have to implemented using javascript. The required function to check the no. of line as well as the no. of characters in a text area may be as:

<script type="text/javascript">

var alert_title='Input Restriction';

function limitTextarea(textarea,maxLines,maxChar){
var lines=textarea.value.replace(/\r/g,'').split('\n'),
lines_removed,
char_removed,
i;
if(maxLines&&lines.length>maxLines){
//alert('You can not enter\nmore than '+maxLines+' lines');
lines=lines.slice(0,maxLines);
lines_removed=1
}
if(maxChar){
i=lines.length;
while(i-->0)if(lines[i].length>maxChar){
lines[i]=lines[i].slice(0,maxChar);
char_removed=1
}
//if(char_removed)alert('You can not enter more\nthan '+maxChar+' characters per line')
}
if(char_removed||lines_removed)textarea.value=lines.join('\n')
}

function watchTextarea(){
document.getElementById('resticted').onkeyup()
}

function set_ie_alert(){
window.alert=function(msg_str){
vb_alert(msg_str)
}
}
</script>

and the above function can be called as

</script>
</head>
<body>
<textarea onkeyup="limitTextarea(this,6,40)" style="width:400px;height:200px" wrap="off" id="resticted" onfocus="focus_watch=setInterval('watchTextarea()',250)" onblur="clearInterval(focus_watch)">some text</textarea>
</body>
</html>

Saturday, August 27, 2011

to solve javascript disable browser

If you use javascript code for your application, you should not depend fully on it for various important checking. Beside it, server side checking also should be implemented. Because, if javascript of the web browser is disabled then, all checking will be stopped with it.

But, there is an another option of the disabling problem of javascript. There is a tag <NOSCRIPT>, which is use when javascript is not found in the browser. And message can be showed inside this <NOSCRIPT> tag. Beside showing message it can also be used to hide a page element from the page. An example of code snap of using <NOSCRIPT> tag. <NOSCRIPT> tag should be used inside <HEAD> tag

Code Example of NOSCRIPT


<noscript >
<style type="text/css">
.pagecontainer {display:none;}
</style>
<div style="color: red; text-decoration: blink;font-size: xx-large" align="center">
This page uses Javascript. Your browser either doesn't support Javascript or you have it turned off.
To see this page as it is meant to appear please use a Javascript enabled browser
</div>
</noscript>