0

I'm using the following function but it does work, can someone please point out what is wrong.

The vb variable is generated from a database query.

window.onload function PostCodeChecker() {
if (<%= sPostalCode %> !== 'SW') {
alert("This is not an SW Postal Code");
}
}
2
  • 1
    Is this the generated code or the one you wrote? How does the generated code look like? You most likely need quotes around <%= ... %>. Commented Nov 9, 2011 at 12:41
  • 2
    If it "does work", then what's the problem? :D Commented Nov 9, 2011 at 12:47

4 Answers 4

2

What's really happening here is that your server-side code is swapping in the value where you've put <%= sPostalCode %>, and sending the result to the browser. So if the sPostalCode were (say) "NW10", you'd get

window.onload function PostCodeChecker() {
if (NW10 !== 'SW') {
//  ^--- ERROR (unless you happen to have a client-side variable by that name, which I'm guessing you don't)
alert("This is not an SW Postal Code");
}
}

So you'll need quotes around it (and you'll need to add the missing =), like this:

//            v-- was missing
window.onload = function PostCodeChecker() {
if ('<%= sPostalCode %>' !== 'SW') {
//  ^--- here          ^--- and here
alert("This is not an SW Postal Code");
}
}

...so that what goes to the browser is:

window.onload = function PostCodeChecker() {
if ('NW10' !== 'SW') {
alert("This is not an SW Postal Code");
}
}

That assumes that the string won't have any ' characters in it, or other characters that would result (when the server-side processing is done and the result sent to the browser) in an invalid JavaScript string literal.

Of course, you could just do this:

<% If sPostalCode <> "SW" Then %>
window.onload = function() {
alert("This is not an SW Postal Code");
}
<% End If %>

...since it's a server-side variable, you can do the test server-side.


Side note: This form of expression:

something = function name() { ... };

...has issues on various browsers, most notably IE. It's called a named function expression because it's a function expression where the function has a name (name, in this case). On IE it results in two completely separate functions being created, which can be ever so confusing. More: Double-take

Sign up to request clarification or add additional context in comments.

Comments

2

The first thing that is wrong is:

window.onload function PostCodeChecker() {

You are missing the assignment operator and are giving it a name (which memory leaks in IE IRRC):

window.onload = function () {

The second thing that is wrong is:

if (<%= sPostalCode %> !== 'SW') {

Which will probably output something like

if (SW !== 'SW') {

And SW is an undefined value. You probably want to wrap the ASP in quotes, and make sure that what you output is JS and HTML safe.

The main thing that is wrong is that you are doing a test on the client, when you could just as easily to it on the server.

Psuedo-code because I don't do ASP:

<% if (sPostalCode == "SW") {
       print 'alert("This is not an SW Postal Code");'; 
   }
%>

(But taking it out of JS entirely and just putting it as text in the page is the usual, and most reliable, approach).

Comments

0

Without quotes it will be looking for a variable with the name of whatever the value of sPostalCode is

Try:

window.onload = function PostCodeChecker() {
    if ('<%= sPostalCode %>' !== 'SW') {
        alert("This is not an SW Postal Code");
    }
}

Comments

0

Two problems.

  1. You forgot = so your syntax is invalid.

  2. You should consider what the resultant Javascript looks like after ASP does its work. sPostalCode is being substituted, so if it has the value "SW" then your conditional ends up like this:

    if (SW !== 'SW')
    

    This is not valid Javascript, because you have no quotes around that string.

To fix:

window.onload = function PostCodeChecker() { // <---- equals
   if ('<%= sPostalCode %>' !== 'SW') {         // <---- quotes in the JS
      alert("This is not an SW Postal Code");
   }
}

^ I also added indentation to make your code more legible. I highly recommend always doing this.

You also should consider what happens when sPostalCode contains any character that will break that Javascript syntax; for example, a '. You should prepare or "escape" the variable in ASP accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.