Showing posts with label js. Show all posts
Showing posts with label js. Show all posts

Monday, December 01, 2008

Parse Int problems with leading 0's

Parse Int problems with leading 0's

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_21701871.html


Trying to convert
string with leading 0 to number and it doesn't work

ie:

var test = '08';
var value = parseInt(test);

I wan't 08 I currently get it to work like this, but there must be a better way.

if (test.charAt(0) == '0') test= test.charAt(1);
value = parseInt(test);

answer=
There is an optional parameter that allows you to specify base ten. If there are leading zeros for some unknown reason to me parseInt will treat the number as an octal number (base 8) just do it like this:

var test = '08';
var value = parseInt(test,10);

...That one pissed me off forever until I found out what was going on.

Glad to help :)

Tuesday, June 26, 2007

Javascript Copy Selected Text Box Select All Highlight Text

The below link is
Javascript Copy Selected Text Box Select All Highlight Text

www.wallpaperama.com

But this will not work in firefox
because document.selection.createRange() is undefined
You need something like
Code:
if (window.getSelection)
{
txt = window.getSelection();
foundIn = 'window.getSelection()';
}
else if (document.getSelection)
{
txt = document.getSelection();
foundIn = 'document.getSelection()';
}
else if (document.selection)
{
txt = document.selection.createRange().text;
foundIn = 'document.selection.createRange()';
}
else return;
Too bad later i found that window.getSelection will not work in textarea.. and yet this is bug for firefox according
http://www.squarefree.com

Code:
function copyit(theField) {

var selectedText = document.selection;

var txt;
var foundIn;

if (window.getSelection)
{
txt=getSelectionInfo(theField);
//alert('ori='+theField.value.length);
//alert('new='+txt.length);
if(txt.length==theField.value.length )
theField.value='';
}
else if (document.getSelection)
{
alert('2='+document.getSelection);
txt = document.getSelection();
foundIn = 'document.getSelection()';
theField.focus();
theField.value = txt;
}
else if (document.selection)
{
alert('3='+document.selection);
if (document.selection.type == 'Text') {

txt = document.selection.createRange().text;
alert('ori='+theField.value.length);
alert('3='+txt.length);
if(theField.value.length==txt.length)
alert('clearAll!!');
foundIn = 'document.selection.createRange()';
theField.focus();
theField.value = txt;
}
}
else return;

;

}
function getSelectionInfo(theField)
{
var rv = "";
var i,x;

x = theField;

rv += x.value.substr(x.selectionStart, x.selectionEnd - x.selectionStart);

if(x.selectionStart==0 && x.selectionEnd==x.value.length)
return rv;
}