admin管理员组文章数量:1425109
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>
本文标签: vbscriptHow do you use Classic ASP string in client side JavascriptStack Overflow
版权声明:本文标题:vbscript - How do you use Classic ASP string in client side Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745443323a2658545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论