The DevCenter has moved!

The Developer Center has a new home, developer.ektron.com. The site you are currently viewing is available as a read-only archive.
Things to note:
  1. Your username & password is the same on the new site, developer.ektron.com
  2. Going forward, please use the new URL, developer.ektron.com
  3. The old discussion forums and old KB articles are still available as an archive (read-only)

Ektron Knowledge Base

The Knowledge Base has thousands of articles describing open and past issues and providing answers to frequently asked questions.

Hover the mouse over to the side bar to open a side menu and expend a node for a list of articles.

INFO:JavaScript Tutorial - Quick Reference

Applies To:
All Ektron Products
Summary:

Introduction | The Language | Object Oriented Programming | Quick Reference | Best Practices | Self Evaluation

Quick Reference

Only some of the JavaScript operators, statements and objects are listed here.


Operators

typeof

Returns the type of the operand. The returned types are:

  • "boolean"
  • "function"
  • "number"
  • "object"
  • "string"
  • "undefined"

Example,

if ("string" == typeof param1 && "somevalue" == param1.toLowerCase())
{
...
}


Statements

for

for (initialize; test; increment)
statement

Example,

var sum = 0;
for (var i = 0; i < a.length; i++)
{
sum += a[i];
}

for/in

The for/in statements loops through the properties of a specified object:

for (variable in object)
statement

Example,

// list properties and their values
for (var p in objObject)
{
alert(p + "=" + objObject[p]);
}

function

function function-name(arguments)
{
statements
}

Example,

function sumsquares(x, y)
{
return x * x + y * y;
}

if/else

if (expression)
{
statements
}
else if (expression)
{
statements
}
else
{
statements
}

switch/case

switch (expression)
{
case constant-expression:
{
break;
}
default:
{
}
}

while

while (expression)
{
statements
}

Note on the with statement: I do not recommend using the with statement because it is very hard to debug. It is not possible to define a new property to an object when using with. Likewise, a misspelled property or a name with the incorrect case is more difficult to find. You may even change the value of a global variable unintentionally when you meant to change a property of the object.


Global Functions

escape(s)

URL encode a string. See unescape.

Example,

strUrl += "?element=" + escape(this.elementName);
if (this.elementWindow)
{
strUrl += "&elementWindow=" + escape(this.elementWindow);
}

eval(code)

Execute JavaScript code from a string. This is one of the most powerful features of JavaScript.

Example,

this.editor = eval(this.editorWindow).document[id];
:
eval('this.editor.' + this.editorSetMethod + '(valueSource.toString())');

parseFloat(s)

Convert a string to a number.

Example,

// use parseFloat to avoid concatentation with "+" operator
alert("4.4 == " + (parseFloat("2.3") + 2.1));

this.browserVersion = parseFloat(ua.substring(pIE + 5));

parseInt(s, radix)

Convert a string to an integer given a radix, typically either 10 (decimal) or 16 (hex).

Example,

// parentheses around parseInt and '2' to avoid concatentation with "+" operator
alert("12 == " + (parseInt("a", 16) + 2));

unescape(s)

Unencode a string that has been URL encoded. See escape.

Example,

objQuery[unescape(pair[0])] = unescape(pair[1]);


Common Window Methods

alert(s)

Display a message in a dialog box with an OK button.

Example,

alert("Only one instance of eWebEditProFactory may be created.");

var sMsg = "Ambiguous element name: " + elemName + "\n" +
"Please specify a form name: " + aryFoundFormNames.toString() + "\n" +
"Example: " + formName + "." + elemName + "\n";
alert(sMsg);

confirm(s)

Ask a yes-or-no, OK-or-Cancel question in a dialog box.

Example,

if (confirm(eWebEditProMessages.querySave))
{
bSaved = this.save();
}

setInterval(code, interval)

Periodically execute specified code every specified number of milliseconds. See note (*).

Example,

setInterval("check()", 3000); // every 3 seconds

setTimeout(code, delay)

Execute code after waiting the specified number of milliseconds. See note (*).

Example,

setTimeout('onExecCommandDeferred("' + sEditorName + '", "' + strCmdName + '", "' + strTextData + '", ' + lData + ')', 1);

setTimeout('eWebEditPro.load("' + objEditor.name + '")', this.delayOnLoadRetry);

* Note on setInterval and setTimeout: A reference to an object cannot be passed to a function called via setInterval or setTimeout. These functions simply perform an 'eval' on the string after a given amount of time.


String Object

+ (concatentate)

Concatenate strings together.

Example,

strText = "<b>" + strText + "</b>".

length

The number of characters in the string.

Example,

if (strText.length > 1).

charAt(n)

Get the nth character from a string. The first character is 0.

Example,

ch = strText.charAt(i).

charCodeAt(n)

Get the numeric code of the nth character from a string. The first character is 0.

Example,

n = strText.charCodeAt(i).

indexOf(substring, start)

Search a string for the specified substring given a starting position. Returns the index where the substring is located. The first character is 0. If the substring is not found, the value -1 is returned.

Example,

this.isWindows = (window.navigator.platform.indexOf("Win") > -1);

var i = name.indexOf(".");
if (-1 == i)
{
aryNames[0] = "";
aryNames[1] = name;
}
else
{
aryNames[0] = name.substring(0, i);
aryNames[1] = name.substring(i + 1);
}

lastIndexOf(substring, start)

Search a string backwards for the specified substring given a starting position. Returns the index where the substring is located. The first character is 0. If the substring is not found, the value -1 is returned.

Example,

var i = name.lastIndexOf("/");

match(regexp)

Returns an array of strings that match the regular expression, or null if there is no match.

Example,

function extractDigits(strText)
{
var s = "";
var a = strText.match(/[0-9]+/g);
if (a)
{
for (var i = 0; i < a.length; i++)
{
s += a[i];
}
}
return s;
}

This can be useful with credit card numbers, phone numbers, social security numbers. Any compound number where you wish to remove the delimiters and any leading or trailing characters.

extractDigits("4269 7123 0412 1828") returns "4269712304121828".

extractDigits("+1 (555) 123-4567") returns "15551234567".

replace(regexp, replacement)

Returns a string with substring(s) matching a regular expression replaced.

Example,

s = s.replace(/'/g, "\\'");

search(regexp)

Search for a regular expression. Returns the index where the substring specified by the regular expression is located. The first character is 0. If the substring is not found, the value -1 is returned. This is one way to do a case-insensitive search.

Example,

var s = "The lazy dog stayed on this side of the fence.";
document.writeln( s.search(/the/) + "<br>"); // case-sensitive, finds the "the" in "the fence"
document.writeln( s.search(/the/i) + "<br>"); // case-insensitive, finds the "The" at the beginning of the string
document.writeln( s.search(/[Ff]ence/) + "<br>"); // finds "fence" or "Fence", but not "FENCE" or other combination of case
document.writeln( s.search(/\bis/) + "<br>"); // not found, no word "is" even though "is" appears in "this".

split(delimiter)

Returns an array of strings from a string.

Example,

var aryQuery = strQuery.split("&");
for (var i = 0; i < aryQuery.length; i++)
{
pair = aryQuery[i].split("=");

substring(from, to)

Returns a substring of a string inclusive of the from character, but exclusive of the to character. The first character is 0.

Example,

var s = "012345";
alert(s.substring(2, 4)); // displays "23"

substr(start, length)

Returns a substring of a string. The first character is 0.

Example,

var s = "012345";
alert(s.substr(2, 2)); // displays "23"

toLowerCase()

Returns a string converted to lowercase.

Example,

s = s.toLowerCase();

toUpperCase()

Returns a string converted to uppercase.

Example,

s = s.toUpperCase();

String.fromCharCode(c1, c2, ...)

Creates a string from character codes.

Example,

s = String.fromCharCode(1578, 1581, 1585);

Alternately, a string can be created as a string literal using \x and \u. Example: s = "\u062a\u062d\u0631";


RegExp Object (Regular Expressions)

Constructor

new RegExp(pattern, attributes)

Example:

var re = new RegExp("pattern", "gi");
var re = /pattern/gi;

Pattern Character Meaning
\n, \r, \t Match literal newline, carriage return, tab.
\\, \/, \*, \+, \?, etc. Match a special character literally, escaping its special meaning.
[...] Match any one character between brackets.
[^...] Match any one character not between brackets.
. Match any character other than a newline.
\w, \W Match any word/non-word character.
\s, \S Match any whitespace/non-whitespace.
\d, \D Match any digit/non-digit.
^, $ Match at beginning/end of string or line.
\b, \B Match at a word boundary/non-boundary.
? Match previous term zero or one time.
+ Match previous term one or more times.
* Match previous term zero or more times.
{n} Match previous term exactly n times.
{n,} Match previous term n or more times.
{n,m} Match at least n but not more than m times.
a | b Match either a or b.
(subexp) Group sub-expression into a single term.
$n In replacement string, substitute the text that matched the n th sub-expression.