简体   繁体   中英

How to use an ASP variable in Javascript?

As the title says, I'm just trying to use an ASP variable in Javascript, but the code that I have is not able to do so. How do I achieve this?

<%
    dim strMyString
    strMyString = "hello there"
%>

<HTML>
<body>

    <%=strMyString%>
    <p id="demo"></p>

<script type="text/javascript">

    document.getElementById("demo").innerHTML = <%=strMyString%>;

</script>

</body>
</html>

The only output I am getting is "hello there". Expected output is "hello there" twice.

Any help is appreciated.

Your example generates the following on the client:

document.getElementById("demo").innerHTML = hello world;

This is obviously invalid JavaScript and will therefor throw a syntax error (check the console).

You have to wrap the string in quotes:

document.getElementById("demo").innerHTML = "<%= strMyString %>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM