473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Request.QueryString in javascript and null parameters

Hi,
I'm writing Javascript code to parse out a query string. I want to
handle the case where a parameter value may not be sent.

So consider a parameter called "State". If the user doesn't pass that
in if I do this:

var state = <%=Request.QueryString["State"]%>

This returns an undefined object that I can't seem to do any checks
against it. So I was told to do this instead (put ticks around the
Request.QueryString):

var state = '<%=Request.QueryString["State"]%>'

This works if State is empty. But it fails if it contains a value.
So how do I handle the possibility that State might not be sent?
Jul 28 '08 #1
2 5476

"Doogie" <dn******@dtgnet.comwrote in message
news:cb**********************************@d77g2000 hsb.googlegroups.com...
Hi,
I'm writing Javascript code to parse out a query string. I want to
handle the case where a parameter value may not be sent.

So consider a parameter called "State". If the user doesn't pass that
in if I do this:

var state = <%=Request.QueryString["State"]%>

This returns an undefined object that I can't seem to do any checks
against it. So I was told to do this instead (put ticks around the
Request.QueryString):
Google for `javascript undefined "test for"`. You could wrap up the test in
an accessor function to handle returning the correct values to plug into
your js.

Tim
var state = '<%=Request.QueryString["State"]%>'

This works if State is empty. But it fails if it contains a value.
So how do I handle the possibility that State might not be sent?

Jul 29 '08 #2
Doogie wrote:
So consider a parameter called "State". If the user doesn't pass that
in if I do this:

var state = <%=Request.QueryString["State"]%>

This returns an undefined object that I can't seem to do any checks
against it.
Suppose the code between `<%=' and `%>' is server-side JScript with ASP
(.NET), there are two probable outcomes if the parameter was not provided:

A) var state = undefined
or
var state = null

In this case the following applies: The server-side application
generates a string representation of the `undefined'/`null' value which
in turn would mean the `undefined' literal (or property of the Global
Object) or the `null' literal client-side.

`undefined'/`null' is _not_ an object, it is a primitive value.
(Contrary to popular belief, not *everything* is an object in current
ECMAScript implementations.) As this value does not represent an object
and is not implicitly converted into one, using a property accessor on it
fails.

B) var state =

In that case the string representation of `undefined'/`null' would be the
empty string or a string containing only whitespace. The result is
client-side code with a syntax error (AssignmentExpression
expected).
So I was told to do this instead (put ticks around the Request.QueryString):

var state = '<%=Request.QueryString["State"]%>'

This works if State is empty. But it fails if it contains a value.
It could only fail in the non-empty case if that value included unescaped
<'characters (which would end the client-side string literal
prematurely) --

var state = ' foo 'bar' '
^ ^
-- or unescaped newlines (which would be a "SyntaxError: unterminated string
literal" client-side):

var state = 'foo
bar';

At least your console/debugger would have shown you that.

So the solution you need is *server-side* escaping of those characters.
Suppose what you posted in `<%= ... %>' is server-side JScript with ASP
(.NET), the following should work:

var state = '<%= Request.QueryString["State"]
.replace(/'/g, "\\$&")
.replace(/(\r?\n|\r)/g, "\\n")) %>';

(Do not be confused by the fact that the expression is not on a single line;
the client-side script engine never sees `<%= ... %>' and the server-side
JScript engine does not care about newlines here.)

It may also be necessary to insert

.replace(/^\s*(undefined|null)\s*$/, "")

before all other replaces, depending on the string representation of the
value retrieved with Request.QueryString["State"].
So how do I handle the possibility that State might not be sent?
You are attempting to cure the wrong problem. It would appear to be best if
you familiarized yourself with the workings of server-side scripting in
general, and your server-side application in particular, first.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jul 29 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laurent Bertin | last post by:
Hi i got a strange problem but it's true i don't make thing like anyone... First Config: + IIS5.0 SP2 (yes i know...) WebSite Security Root : Digest Authentication, NT Authenticated SubFolders...
8
by: George | last post by:
VS.NET 2002/VB Is it possible to retrieve HTML form variables that are hidden input types using Request.QueryString() and/or Request.Form()? I have tried various ways of using those, but to no...
3
by: Axel Dahmen | last post by:
Hi, I've created an aspx page taking a query string argument as parameter. Although I've correctly escaped umlauts in the query string, they do not appear in the QueryString collection. If I...
8
by: Angel | last post by:
I am trying to access in Javascript within an ASP.NET web page the Request Object. I am specifically trying to access the values in the Request.QueryString. When I try to access the values I get an...
3
by: james | last post by:
Hey Guys, Do you have any idea how to acces Request.QueryString in a MasterPage? I need to use it to modify my sitemap. My code is: public partial class MasterPage :...
5
by: =?Utf-8?B?QVRT?= | last post by:
PRB JavaScript in ASP with Request.Form Please help, I'm having a problem with JavaScript in ASP, where the ASP page crashes when I try to determine if data was posted from a FORM or through a...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Make CStr for JavaScript on ASP w/ Request.Form and QueryString In ASP, Request.Form and Request.QueryString return objects that do not support "toString", or any JavaScript string...
14
by: FMDeveloper | last post by:
Currently transitioning from a shared host to a dedicated server. The same code that works on the old server is not working on the dedicated server. It is a simple AJAX request like: <code>...
12
by: Peter | last post by:
Hi if I have a url/query like "localhost?a&b&c=123" then I thought I could get these parameters and values by using NameValueCollection query = HttpContext.Current.Request.QueryString; But if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.