Create Custom Object In Javascript! very cool.

Category : JQuery/JScript

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!)!!!

Javascript custom Intellisense

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);
       }
 
  }

Javascript Is Numeric? Use parseInt() to test!

Category : JQuery/JScript

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)

Testing if a Javascript function exists….

Category : JQuery/JScript

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

Inline Javascript way to make a textbox always UpperCase

Category : JQuery/JScript

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.

Clearing out a type=file in Javascript.

Category : JQuery/JScript

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()”>

Javascript function that will only allow certain file types.

Category : JQuery/JScript

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???

Video Poker – the power of Javascript and the fun of JQuery

Category : JQuery/JScript


card 1 hold

card 2 hold

card 3 hold

card 4 hold

card 5 hold
SCORE: 100
5 points per game – press Deal to begin

I have been going CRAZY over my newly discovered passion and joy – that being JQeury (and thus my reunion with Javascript) .

I intend on going into alot more detail very soon but for right now just know that JQuery is a Javascript library (very small and lightweight) and allows you to do some very cool Javascript stuff that would normally take many more lines of code. … Ya, just like the .Net Framework.

I put together an external page to show off just a VERY VERY few little cool things it can do ( HERE -please ignore the .Net stuff. I also built this page in asp.net just to see how they would interact).

I also dusted off an old Javascript book and followed a tutorial in the back of said book to produce a neat little Javascript poker game – (I also added just a little JQuery in the game — watch the fading score and messages).

This game is completely created from Javascript and was totally quick and easy to do!

This game would be SOOO easy to extend with some Very cool effects using JQUERY ( please feel free to take the code and let me know if you do some cool stuff with it).

One thing I thought would be neat would be to create a High Score cookie that keep the user’s best 5 scores or something like that. SUGGESTIONS or comments are welcome!

peace.

Javascript – parsing variables from the URL

Category : JQuery/JScript

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!!!!