Click to See Complete Forum and Search --> : undefined check in javascript


sachin
06-30-2003, 01:37 PM
Hi,

Actually I am reading the url to get name value pairs from html file through a javascript method,based on some conditions ,One of the name,value can exists or not.

for example: in the below url which i have given does not
have "book" as the name in the url.
so i need to check if it is there or not, if it is not there
then i need to pass null value, some-thing like i need
to check for "undefined".

Let me know how can this be achieved.

below is the sample code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
function jsGetUrl()
{
var oArgObj = new Object();
//var sParams = location.search.substring(1);
var sParams = "http://127.0.0.1:8080/aal/view/view.htm?id=091111abcdef0001&toc=y&p=091111abcdef0001/520608.003/TPSEC04.pdf@xml=http://127.0.0.1:8080/vtopic.isapi?action=view&Vdkvgwkey=../../aaa/content/091111abcdef0001/520608.003/TPSEC04.pdf&doctype=xml&collection=vc1&Queryzip=sql"
var sPairs = sParams.split("&");
var sPos, sArgname, sValue;
for (var iCnt = 0; iCnt < sPairs.length; iCnt++)
{
sPos = sPairs[iCnt].indexOf('=');
if (sPos == -1) continue;
sArgname = sPairs[iCnt].substring(0,sPos);
sValue = sPairs[iCnt].substring(sPos+1);
if (sValue.indexOf('@') != -1 )
{
var aTemp = sPairs[iCnt].split("@");
sValue = aTemp[0].substring(sArgname.length + 2);
oArgObj[sArgname] = sValue;
sArgname = "xml" ;
sValue = aTemp[1].substring(sArgname.length + 1);
}

oArgObj[sArgname] = sValue;
}
return oArgObj;
}

var oGetArgs = jsGetUrl();

var utest = oGetArgs.book;
var sBooktxt;
if (typeof utest == "undefined")
sBooktxt = "" ;
else
sBooktxt = oGetArgs.book;

alert("final " + sBooktxt);

//--></SCRIPT>



</body>
</html>

Charles
06-30-2003, 02:09 PM
Here's a simple little Object class that I tossed together a while back:

function Query () {
if (self.location.search) {
this.query = self.location.search.replace(/^\?/, '');
var pairs = this.query.split('&');
for (i=0;i<pairs.length;i++) {if (/(.*)=(.*)/.test(pairs[i])) this[unescape(RegExp.$1)] = unescape(RegExp.$2)};
}
}

Query.prototype.toString = function () {return this.query}

To use it, create a Query object with query = new Query(). Then if you want to alert the value of "book" but only if "book" exists you simply use if (query.book) alert(query.book).

sachin
06-30-2003, 02:31 PM
Hi,

But i am not sure whether book exists or not.
so when book does not exists, query.book gives
me undefined, so how can handle or do some-thing
when query.book is "undefined"


Regards,
Sachin

Charles
06-30-2003, 02:35 PM
if (!query.book) alert('Book is undefined')

Or, if you prefer:

alert(query.book ? query.book : 'Book is undefined')

pyro
06-30-2003, 02:38 PM
Look at http://www.infinitypages.com/research/querystring.htm You could set you own alert (rather than "undefined key"). I wrote it to create custom get() function. Somewhat similar to the $_GET["varname"] in PHP...