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!
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!
Share Improve this question edited Nov 5, 2014 at 12:22 user692942 16.4k8 gold badges84 silver badges190 bronze badges asked Nov 4, 2014 at 20:56 PhiPhi 5144 gold badges13 silver badges32 bronze badges2 Answers
Reset to default 5b = "'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.
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 acplish 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>