You can define your own objects in Javascript that will hold properties and perform calculations.
Additionally, as if that is not cool enough (dang I love Javascript), you can also define methods/functions that are attached to those objects (see below).
AND additionally, as if THAT is still not cool enough, if you are using Visual Studio 2010 (not sure about 2008) – you will get ‘ intellisense’ for these new Javascript objects (he laughs uncontrollably from pure geek joy!)!!!

Notice ID and Name are properties and MultiplyBy1000 is my custom made method for this object.
//Create an array to hold your objects
var jsObjectList = new Array();
//You can define objects in Javascript like you would any JS function
function myJSObject(num1, num2, myName) {
this.num1 = num1;
this.num2 = num2;
this.myName = myName;
this.combinedNums = num1 * num2;
}
//You can create methods and functions (that return values) for these objects by using the PROTOTYPE keyword.
myJSObject.prototype.MultiplyNumberBy1000 = function(numberToMultiplyBy1000)
{
alert (numberToMultiplyBy1000 * 1000);
}
//Make one of your spiffy new objects and then add it to your array!
function createOneOfMySpiffyNewObjectsAndAddItToMyArray() {
//Instantiating new obj
var newObject = new myJSObject( 2, 12, 'Todds');
//Adding it to array
if (jsObjectList.length == 0)
jsObjectList[0] = newObject;
else
jsObjectList[jsObjectList.length] = newObject;
}
//Now you can iterate through your array and grab values from your list of objects like this!
function iterateMyObjectArray() {
for (i = 0; i < jsObjectList.length; i++) {
var currentNewObject = jsObjectList[i];
//Now pull info from that object like this...
var a = currentNewObject.num1;
var b = currentNewObject.myName // etc, etc.
//And call methods like this
currentNewObject.MultiplyBy1000(4);
}
}
You can use JavaScript’s built-in parseInt() function to easily test for a numeric value:
if (myTextboxValue != parseInt(myTextboxValue))
alert(myTextboxValue + ' is not a whole number');
parseFloat will also allow you to get test if the data is a number (including floats)
Believe it or not, you may come up against a situation where you do not whether a Javascript function exists in your page or not…. here is my scenario.
I am using usercontrols that are added to the page IF certain settings are set in the webconfig (something you might do if someone has one of your apps installed or has purchased a certain portions of your app) … so its possible this usercontrol has not been added to my page.
SO I need to know if I have a certain javascript function so that I can call it…. however if I call it or test for it WRONG I will get a Javascript error.
Here is the simple way to test this situation:
if(window.myJavascriptFunction) {
//do whatever I want to do now… like just CALL IT!
myJavascriptFunction();
}
hope this helps!
t
onBlur=”javascript:this.value=this.value.toUpperCase();”
Of course, this could be done the opposite way with .toLowerCase();
This forces the textbox to all upper case as the user leaves, works well.
IF you need to clear a type=FILE after you the user has chosen the a file…
you need to surround the input with a span – and then on your clear function,
just overwrite the span with a NEW input type-file box.
This needs to be done because in IE, you will not be able to just clear out text with
filebox.value = ” .
function clearMe() {
document.getElementById(“test”).innerHTML=”<input type=\”file\” id=\”fn\”>”;
}
Picture : <span id=”test”><input type=”file” id=”fn” ></span>
<input type=”button” value=”Clear” onClick=”clearMe()”>
I found this on the asp.net site by a John Stodola (site here: http://blog.josh420.com/)
It’s a simple function, but very useful…thought I would spread the word (thanks John!)
function checkFileExtension(elem) {
var filePath = elem.value;
if(filePath.indexOf(‘.’) == -1)
return false;
var validExtensions = new Array();
var ext = filePath.substring(filePath.lastIndexOf(‘.’) + 1).toLowerCase();
validExtensions[0] = ‘jpg’;
validExtensions[1] = ‘jpeg’;
validExtensions[2] = ‘bmp’;
validExtensions[3] = ‘png’;
validExtensions[4] = ‘gif’;
validExtensions[5] = ‘tif’;
validExtensions[6] = ‘tiff’;
validExtensions[7] = ‘txt’;
validExtensions[8] = ‘doc’;
validExtensions[9] = ‘xls’;
validExtensions[10] = ‘pdf’;
for(var i = 0; i < validExtensions.length; i++) {
if(ext == validExtensions[i])
return true;
}
alert(‘The file extension ‘ + ext.toUpperCase() + ‘ is not allowed!’);
return false;
}
NOW I AM LOOKING FOR A FUNCTION THAT LITERALLY FILTERS THE FILES
OUT IN THE FILES WINDOW LIKE THE .NET FILTER CAN.
ANYONE???
First off this article is to let more people know about this fantasticly sweet Javascript function that I found here : http://www.netlobo.com/url_query_string_javascript.html (this site has some very good articles…check them out!).
Apparently there is no good way to retrieve variables that you have via the url string in plain old Javascript.
However this function (again written by netlobo.com) — creates the solution:
function parseURL( name ) {
name = name.replace(/[\[]/,”\\\[").replace(/[\]]/,”\\\]”);
var regexS = “[\\?&]“+name+”=([^&#]*)”;
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return “”;
else
return results[1];
}
By using some sweet RegEx action – we can pass this function the name of the variable that we want out of the URL and WHALAH! out it comes. For example, I used this function to pull a userName variable that I had passed out of the url.
My url looked like this myPage.aspx?userName=ToddV
I wanted to grab the userName and so wrote this function call.
var userNameInUrl = parseURL(‘userName’);
Now userNameInUrl equals ToddV! … Brilliant. Thanks netLobo.com!!!!