JavaScript

From CDYNE Wiki
Jump to: navigation, search

JavaScript and Web Service

JavaScript is a widely-used general purpose scripting language that is specially designed to work on the Web and very well suited for web development. For this example we will consume a webservice with javascript. This example uses CDYNE's Phone Notify! Web Service.

Usage

The Status text areas are as follows:

  1. TopStatus = a div tag with an ID of TopStatus
  2. StartTime = a div with an ID of StartTime
  3. EndTime = a div with an ID of EndTime

Call the Dial function and the system will send out a CDYNE Notify!

Source Code

function Dial(numbertodial, texttosay, callerid, CallerIDName,Voice, Key)
{
 
 
var url = "http://ws.cdyne.com/NotifyWS/PhoneNotify.asmx/NotifyPhoneBasic?PhoneNumberToDial=" +
escape(numbertodial) +
"&TextToSay=" + escape(texttosay) +
"&CallerID=" + escape(callerid) +
"&CallerIDname=" + escape(CallerIDName) +
"&VoiceID=" + Voice +
"&LicenseKey=" + Key;
request = new XMLHttpRequest();
request.onreadystatechange = myStatusProc;
request.open( "GET", url, true );
request.send();
}
 
function myStatusProc()
{
if ( request.readyState == 4)
{
if ( request.status == 200 )
{
var XML = request.responseXML;
var nodeString = "";
 
var nodeList = XML.documentElement.getElementsByTagName("ResponseCode");
var node = nodeList.item(0);
var RC = node.firstChild.nodeValue;
 
if( RC==0 )
{
var nodeList = XML.documentElement.getElementsByTagName("QueueID");
var node = nodeList.item(0);
QueueID = node.firstChild.nodeValue;
TopStatus.innerText = "Queued";
 
setTimeout("CheckNotify()",1000)
}
else
{
var nodeList = XML.documentElement.getElementsByTagName("ResponseText");
var node = nodeList.item(0);
var RT = node.firstChild.nodeValue;
TopStatus.innerText = RT;
else if(RC==13)
{
TopStatus.innerText = "CallerID and Number cannot match";
}
}
}
}
}
 
function CheckNotify()
{
request = new XMLHttpRequest();
request.onreadystatechange = CheckProc;
request.open("GET", "http://ws.cdyne.com/NotifyWS/PhoneNotify.asmx/GetQueueIDStatus?QueueID=" +
QueueID, true );
request.send();
 
}
 
 
function CheckProc()
{
 
if ( request.readyState == 4)
{
if ( request.status == 200 )
{
var XML = request.responseXML;
var nodeString = "";
 
var nodeList = XML.documentElement.getElementsByTagName("ResponseCode");
var node = nodeList.item(0);
var RC = node.firstChild.nodeValue;
var nodeList = XML.documentElement.getElementsByTagName("ResponseText");
var node = nodeList.item(0);
var RT = node.firstChild.nodeValue;
var nodeList = XML.documentElement.getElementsByTagName("Duration");
var node = nodeList.item(0);
var dur = node.firstChild.nodeValue;
 
TopStatus.innerText = RT;
 
if( RC==1 )
{
 
var nodeList = XML.documentElement.getElementsByTagName("MachineDetection");
var node = nodeList.item(0);
var MD = node.firstChild.nodeValue;
TopStatus.innerText += " by " + MD;
 
var nodeList = XML.documentElement.getElementsByTagName("StartTime");
var node = nodeList.item(0);
var ST = TimeStampToDate(node.firstChild.nodeValue);
StartTime.innerText = "Start: " + ST.toLocaleTimeString();
 
}
 
if( dur > 0 )
{
// stop timing and show EndTime
var nodeList = XML.documentElement.getElementsByTagName("EndTime");
var node = nodeList.item(0);
var ET = TimeStampToDate(node.firstChild.nodeValue);
EndTime.innerText = "End: " + ET.toLocaleTimeString();
}
else
{
if (RC != 2)
t=setTimeout('CheckNotify()',3000) // every 3 seconds
}
 
}
}
}
 
 
function TimeStampToDate(xmlDate)
{
var dt = new Date();
var dtS = xmlDate.slice(xmlDate.indexOf('T')+1, xmlDate.indexOf('.'))
var TimeArray = dtS.split(":");
dt.setUTCHours(TimeArray[0],TimeArray[1],TimeArray[2]);
dtS = xmlDate.slice(0, xmlDate.indexOf('T'))
TimeArray = dtS.split("-");
dt.setUTCFullYear(TimeArray[0],TimeArray[1],TimeArray[2]);
return dt;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Tools

Trial Key