Simple Parameter Processing

D

DougJrs

Good Morning,

I am trying to write a simple function that would grab the "errorId"
parameter and then display a message when the page loads. I basically
have a login page (login.asp) that if the login fails the user is
directed page to the login page with an errorId parameter (login.asp?
errorId=1).

I wrote the javascript below to grab the paramater and then retuen a
message (I borrowed the gup function from an example, and wrote the
getMessage)

Now, my problem is that I am not sure how to get the message returned
by getMessage displayed on the screen. I think that adding the
function to window.onload would call it when the page loads, but I
don't know how to get the text written out to the browser window.

Thanks in advance for any help!
Doug

function gup( 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];
}

function getMessage( )
{
var error = gup("errorId");
if (error == "") return "";
else return "Email address and password combination not found.";
}
 
T

Thomas 'PointedEars' Lahn

DougJrs said:
I am trying to write a simple function that would grab the "errorId"
parameter and then display a message when the page loads. I basically
have a login page (login.asp) that if the login fails the user is
directed page to the login page with an errorId parameter (login.asp?
errorId=1).

I wrote the javascript below to grab the paramater and then retuen a
message (I borrowed the gup function from an example, and wrote the
getMessage)

But, while that might have been a good training for a general client-side
solution for parsing the query string (which is seldom necessary anyway),
it is far too complicated in your case.
function gup( 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];
}

That code strikes me as being unnecessarily complicated, and error-prone.

For example, there will not be an unescaped "[" or "]" character in the
query part of a Valid URI (see RFC3986, 2.2.) However, if we assume that
the method can be passed a string containing such characters, precautions
must be taken for other reserved characters, too.

function getFirstQueryParam(p)
{
var q = window.location.search;
if (typeof q == "string")
{
var m = q.match(
new RegExp("[\\?&]?" + encodeURIComponent(p) + "=([^&#;]*)"));
if (m) return decodeURIComponent(m[1]);
}

return false;
}

var error = getFirstQueryParam("errorId");

See also http://PointedEars.de/scripts/search.js
Now, my problem is that I am not sure how to get the message returned
by getMessage displayed on the screen. [...]

Instead of
function getMessage( )
{
var error = gup("errorId");
if (error == "") return "";
else return "Email address and password combination not found.";
}

you simply write something along (assuming JScript or JScript.NET server-side):

<%
if (condition)
{
%>
Email address and password combination not found.
<%
}
%>

You may include that anywhere in your markup, even in generated client-side
script code.

That is, in the case of an error you do not first serve much the same
resource and then retrieve the error code from the query part, but you
serve a different resource in the first place. Which can remove the
necessity for using client-side scripting there.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top