0

I've noticed that you can set javascript variables using ASP code. For example:

var test = "<%response.write(number)%>"

I was wondering if other types of ASP code can work in javascript, such as if statements or while loops. For Example:

function test1()
{
   count = 0;
   <%if number = 1 then%>
        count = count + 1;
   <%end if%>
}

function test2()
{
   count = 0;
   <%index = 0
   do while index < 10 %>
       count = count +1;
   <%index = index +1
   loop%>
}

I am relatively new to web development and programming so I'm not sure if this is possible. If this does not work, is there any way I can get around this or a different way to code it?

Any tips or advice would be greatly appreciated.

4
  • 5
    You really need to understand the difference between client-side processing and server-side processing... they achieve two quite different things Commented Jul 30, 2012 at 13:59
  • 2
    Server side - ASP, PHP and similar, this code is "compiled" by the web server before it gives the user the webpage, so things like asp variables are set at this point. Client side - Javascript, this retrieves the web page as the server sent it and then manipulates it, so yes, you can set javascript variables with ASP Commented Jul 30, 2012 at 14:01
  • Sorry @user1538100, but that is not entirely correct... not all server-side is compiled (ASP.NET markup for instance), but it is processed on the server Commented Jul 30, 2012 at 14:02
  • 2
    Used compiled in quote marks as I couldn't think of the proper word (as you put it, processed) apologies x Commented Jul 30, 2012 at 14:19

1 Answer 1

2

As I say in my comment, it's really important to understand the difference between client-side and server-side processing in things like ASP, ASP.NET, PHP, etc.

As you appear to know, the javascript is run on the client-side (i.e. the browser). The server-side does processing of the ASP, ASP.NET, PHP, etc and then sends information (HTML, Javascript, etc) to the browser.

You can indeed do the following code that you have written...

function test2()
{
   count = 0;
   <%index = 0
   do while index < 10 %>
       count = count +1;
   <%index = index +1
   loop%>
}

But instead of the SERVER doing the calculation 10 times, the server will create the line 10 times and send it to the browser...

function test2()
{
   count = 0;
       count = count +1;
       count = count +1;
       ... followed by another 8 lines of the same
}

... meaning the CLIENT run javascript will do the calculation 10 times.

Depending on exactly what you need the code to do will depend on whether the code should be run client-side or server-side... it's pretty much impossible to tell you which without knowing what you're trying to do.

Hope this helps

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

13 Comments

I see. This helps out a lot. Thank you very much for your input!
You're welcome @blazinazian - client-side vs server-side can take a bit of getting your head around! Good luck with the rest of your project
Is it possible for me to send you a copy of what I have now? Because the concept is there, but nothing is happening and I'm wondering if I'm getting client-side vs server-side mixed up again.
Of course @blazinazian, the best thing to do would be to chat, but I'm trying to work out how to initiate it (before it's suggested due to too many comments)
Ok. Please let me know when the chat has been initiated.
|

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.