1

I am trying to develop a simple back-end widget for asp. Since, I am new to ASP, I chose JavaScript as ASP language. I think I don't have the right tool to write to output. Response.Write() sends output directly to the start of the page. What I am missing here? Hers is the code that I made:

<!DOCTYPE html>
<html>
<body>
<h2>Hello world</h2>
<script language=Javascript runat=server>
    Response.Write("Hello JS");
</script>
</body>
</html>

which is giving the following output:

Hello JS    <!DOCTYPE html>
    <html>
    <body>
    <h2>Hello world</h2>

    </body>
    </html>
user692942
  • 16,398
  • 7
  • 76
  • 175
cnvzmxcvmcx
  • 1,061
  • 2
  • 15
  • 32
  • Better use, `<%=%>` and not place the script run on server. Also you try asp, or asp.net ? are not the same – Aristos Apr 24 '14 at 07:47
  • Have you tried something like : <%@ language="javascript"%> on top of your page? And put your output something like <% Response.Write("Hello World!") %>? Beside that, I wouldn't invest much time in researching ASP, since this is really vintage technology. – Steven Apr 24 '14 at 10:05
  • 1
    I think you'll find [this answer](http://stackoverflow.com/a/1449277/692942) clears up any mis-conceptions about `runat="server"` and `<% %>` (ASP processing tags) and the order they are executed. – user692942 Apr 24 '14 at 13:45

2 Answers2

1

There is a great answer on the subject here but basically if you want this to work change the above code block as follows;

<!DOCTYPE html>
<html>
<body>
<h2>Hello world</h2>
<% Call Response.Write("Hello JS"); %>
</body>
</html>

You can also replace the above line of code with <%= "Hello JS" %> as a shortened form of Response.Write() method.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
-2

if you want to use javascript then it should be like this:

<script language=Javascript>
     alert("Hello JS");
 </script>

or

 <script language=Javascript>
     alert('<%=SomeVarfromASP%>');
 </script>

Those will give you POP up boxes with your message or in your case value of your variable in it.

Basically when you put "<%=" it is pretty much like you say please write this... If you need something to be typed/printed on the page by JavaScript you will need to use more specialized functionality such as getelementbyid or similar. Your code would look like:

<html>
  <head>
   <script type="text/javascript">
      function ChangeGreet()
        {
          var vgreet = document.getElementById("JSGreet");                                                 
          vgreet.innerHTML = 'Hello JS';

         }
     </script>
   </head>
   <body onload="ChangeGreet()">
       <div>Hello MS</div>
       <div id="JSGreet">     </div>
   </body>
 </html>

JavaScript and VBScript(Classic ASP) have their own syntax. Look it up at http://www.w3schools.com/js/DEFAULT.asp. It has great tutorials for beginners in both languages.

All Blond
  • 802
  • 1
  • 8
  • 16
  • @VibhavSinha One more source for your JavaScript practice: http://www.tizag.com/javascriptT/index.php – All Blond Apr 24 '14 at 14:56
  • He wasn't asking for a client side approach the Javascript was being used server side, note the `runat=server` syntax. The issue is the OPs expectation of the order in which server side ` – user692942 Apr 25 '14 at 00:03
  • Also `alert()` is "client side" Javascript and will not work in "server side" code. – user692942 Apr 25 '14 at 00:05
  • @Lankymart: personal reasons is not a good foundation to down-vote answers. Are you sure that you motivated by professional skills and not by some other reasons when down-voting all my answers, even those which has been accepted as best answers to OP request? Should I ask moderators to look on your activity? – All Blond Apr 25 '14 at 13:45
  • Sorry I've downvoted your answer? You got some godlike abilities I'm not aware of? If I was to downvote your answer there would be plenty of reason as I stated in my previous comment. Your answer makes no sense you talk about `alert()` in the context of code run server side. If you want to make this personal go ahead, but SO doesn't lend itself to this type of interaction. – user692942 Apr 25 '14 at 13:50
  • So stop it.I did my best to ignore your abusive comments but enough is enough.You want to chat, go ahead and use chat room not this place. – All Blond Apr 25 '14 at 13:52
  • Stop what? The answer is incorrect if you don't expect someone to point that out then you're on the wrong website. That's probably why it has been downvoted. Seriously I have better things to do then get into a slanging match with you. If I've offended you I apologise. – user692942 Apr 25 '14 at 13:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51438/discussion-between-lankymart-and-all-blond) – user692942 Apr 25 '14 at 14:27