Translator
Categories
Archives

Archive for February, 2009

Get QueryString Values in JavaScript

I have had several people ask me how to get URL QueryString values in JavaScript, so I created a function that will take a string, find the matching QueryString key, and return the value associated with that key. Enjoy!

NOTE: You may have to remove and re-type the “s from the code below if you copy it from this page. Some of them don’t come through correctly when you paste into your dev application.

Let's pretend the URL of this page is http://www.noobflash.com/foo/index.html?noob=someValue. The code above will find the first QueryString instance of "noob" in the URL and return the "someValue" portion to be used in your code. You would simply type queryStringValue("stringToFind") whenever you would like to get new QueryString data.

SQL If / Else Statement

A request has been made for me to cover how to create a SQL If / Else statement. In this example, I am declaring a variable (@myVar), and using a select statement to set it equal to the value of table.columnName. Once you have a value, you can use that value in your If / Else statement. This example is checking to see if @myVar is greater than 10. If so, then the first SQL select statement is executed. If @myVar is not greater than 10, then the second SQL statement is executed.

DECLARE @myVar AS INTEGER

SELECT
@myVar = table.columnName
FROM table
WHERE columnName2 = condition

IF ( @myVar > 10 )
BEGIN
   SELECT *
   FROM table
   WHERE columnName = @myVar
END
ELSE
BEGIN
   SELECT *
   FROM table
   WHERE columnName < @myVar
END

Load XML in Flash (AS3)

XML is the most popular data delivery methods for Flash and Flex. It’s fairly simple to load and consume XML, but it’s a little intimidating if you’re a ActionScript noob. Use the following code to pull in xml data and display it via a Flash trace.

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, getXML);
// Request XML File from URL
loader.load(new URLRequest("http://www.noobflash.com/feed/"));

function getXML(event:Event):void {
	var xmlData:XML = new XML(event.target.data);
	trace(xmlData);
}

Once you have the XML loaded, you will need to parse the XML using E4X. (Read: Parse XML Data in ActionScript 3 Using E4X)

Set Style Dynamically With JavaScript

Changing the style of an object is as simple as finding the object on the page, then setting the new style value. Use the code below as an example.

//Find the div you want to update
var divArray = document.getElementById('myDiv');
//Set the width style
divArray.style.width = '300px';

CSS attribute names are slightly different when used in JavaScript, so you will need to reference the list below before you get frustrated and pull your hair out.

CSS Attribute to JavaScript Reference
CSS Property JavaScript Reference
background background
background-attachment backgroundAttachment
background-color backgroundColor
background-image backgroundImage
background-position backgroundPosition
background-repeat backgroundRepeat
border border
border-bottom borderBottom
border-bottom-color borderBottomColor
border-bottom-style borderBottomStyle
border-bottom-width borderBottomWidth
border-color borderColor
border-left borderLeft
border-left-color borderLeftColor
border-left-style borderLeftStyle
border-left-width borderLeftWidth
border-right borderRight
border-right-color borderRightColor
border-right-style borderRightStyle
border-right-width borderRightWidth
border-style borderStyle
border-top borderTop
border-top-color borderTopColor
border-top-style borderTopStyle
border-top-width borderTopWidth
border-width borderWidth
clear clear
clip clip
color color
cursor cursor
display display
filter filter
font font
font-family fontFamily
font-size fontSize
font-variant fontVariant
font-weight fontWeight
height height
left left
letter-spacing letterSpacing
line-height lineHeight
list-style listStyle
list-style-image listStyleImage
list-style-position listStylePosition
list-style-type listStyleType
margin margin
margin-bottom marginBottom
margin-left marginLeft
margin-right marginRight
margin-top marginTop
overflow overflow
padding padding
padding-bottom paddingBottom
padding-left paddingLeft
padding-right paddingRight
padding-top paddingTop
page-break-after pageBreakAfter
page-break-before pageBreakBefore
position position
float styleFloat
text-align textAlign
text-decoration textDecoration
text-decoration: blink textDecorationBlink
text-decoration: line-through textDecorationLineThrough
text-decoration: none textDecorationNone
text-decoration: overline textDecorationOverline
text-decoration: underline textDecorationUnderline
text-indent textIndent
text-transform textTransform
top top
vertical-align verticalAlign
visibility visibility
width width
z-index zIndex

Get Character Length of a String with JavaScript

Occasionally I run across the need to get the number of characters in a string. You can use the code below to determine the character length of a string.

var myString = 'This is a String';
alert(myString.length);