2

I have an asp classic page and when I try to use an asp variable inside JavaScript code it only permits numbers, when a string is in the variable it sends an error:

<%
dim a, b
    a = "11322353464767875689"
    b = "someId=1&another=29"
    %>

    <script language='javascript'> 
        var urlParamsJS = <%=b%>
        alert(urlParamsJS);
        var params = 'dependent=yes,directories=no,hotkeys=no,menubar=no,personalbar=no,scrollbars=yes,status=no,titlebar=yes,toolbar=no';

        var fullurl = 'page.asp?p=' + urlParamsJS

        var wcontainer2  = open ( fullurl, 'otherThings', params); 
        wcontainer2.resizeTo (900,470);
        wcontainer2.moveTo (100,220);
    </script>");

When I use <%=b%> only prints to screen ");

But when i use <%=a%> I get an alert with this text page.asp?p=11322353464767875689

Help please!

2 Answers 2

5
b = "'someId=1&another=29'"

or

var urlParamsJS ='<%=b%>'

Select the one that better fits with your code.

If you want to assing a string value to a variable (what you are doing with <%=b%>), the value must be quoted.

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

2 Comments

Or var urlParamsJS = "<%=b%>"; it's all personal preference.
@Lankymart, the quote selection was done based on the existing code in the OP question. As single quotes are used in its javascript code, single quotes are used in the answer. Anyway, of course, you are right, the language allows you to use one quoting style, the other or both.
2

Although @MC ND is right with their answer it lacks a bit of detail as to why this is the case.

Server-Side / Client-Side

Classic ASP is a server side processing technology, in basic terms what you write to the client (Internet Browser or other client that consumes HTML over HTTP) using <% Response.Write() %> or it's shorthand equivalent <%= %> is being sent exactly as it was typed.

Take the example of your question, the server side variable

b = "someId=1&another=29"

If you used <%= b %> or <% Response.Write b %> the output would be the same

someId=1&another=29

This is because the Classic ASP sends the result of the literal string someId=1&another=29. The quotes (") that make up a string in VBScript are nothing to do with the outputted result.

Debugging the Problem

Usually a good way of checking this is using View Page Source or equivalent function on your client, to view the raw output sent by the Web server. If you had you would have seen something like this;

<script language='javascript'>
    var urlParamsJS = someId=1&another=29
    alert(urlParamsJS);
    var params = 'dependent=yes,directories=no,hotkeys=no,menubar=no,personalbar=no,scrollbars=yes,status=no,titlebar=yes,toolbar=no';

    var fullurl = 'page.asp?p=' + urlParamsJS

    var wcontainer2  = open ( fullurl, 'otherThings', params); 
    wcontainer2.resizeTo (900,470);
    wcontainer2.moveTo (100,220);
</script>

In JavaScript this assignment (var urlParamsJS = someId=1&another=29) is incorrect and will trigger an exception (depending on your client settings this may not be obvious) and your client-side code will not run as intended.

Tell the Client-Side Your Dealing with a String

The easiest way to accomplish this is by forcing it in your client-side code.

<script language='javascript'>
    // Surrounding with "" or '' tells the client this is a string.
    var urlParamsJS = '<%= b %>';
    alert(urlParamsJS);
    var params = 'dependent=yes,directories=no,hotkeys=no,menubar=no,personalbar=no,scrollbars=yes,status=no,titlebar=yes,toolbar=no';

    var fullurl = 'page.asp?p=' + urlParamsJS

    var wcontainer2  = open ( fullurl, 'otherThings', params); 
    wcontainer2.resizeTo (900,470);
    wcontainer2.moveTo (100,220);
</script>

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.