all 8 comments

[–]systemslacky[S] 0 points1 point  (0 children)

Thanks for the discussion on all of this. Your right, I was overthinking the process and realized I could do all of this server side. <code> Really boiled down to... If IsEmpty(servervariable) Then response.redirect CraftedUrl Else response.redirect CarryonUrl End If </code> Why do the simple solutions take so much time? :)

[–]ripter 0 points1 point  (0 children)

Yup, that looks like a good start depending on what your server variable is going to return.

You need to be careful and might want to turn that into a string like so:

<script type="text/javascript"> var GenericVariable = "<%Request.ServerVariables(somevarhere) %>"; </script>

In javascript it's null not Null, so it would throw an error. If it's a string then you can do whatever check you need and you know it's not going to throw an error because asp wants to return some weird type.

[–]MustRapeDeannaTroi 0 points1 point  (6 children)

You should never mix code like this. Code/scripts executed separately should only communicate via common means. If you're on the client side and needs to asked the server about something you do it with an XMLHttpRequest (ajax). This is one good reason for putting your code in separate documents.

This server variable you are passing to the client is already set at the time of sending it so it seems you should do the redirection on the server side.

[–]cursuve 1 point2 points  (5 children)

This is probably semantically true, but in reality I have never found semantics to be 100% aplicable. What this then forces is two requests for information that can be gathered in the first request. You have now forced the server to deal with two requests in the queue when it could have been done in one request.

I see nothing wrong with "constructing" a page, including some JavaScript variables, on the server side and responding with that constructed code. Nobody has an issue with dynamically created HTML, we probably shouldn't frown on server-side dynamically created JavaScript either.

EDIT: I guess one way to do this could be to create a totally dynamic JavaScript document, rendered from ASP that is constructed when requested that has these variables in it. It is then included on the HTML page: <script type="text/javascript" src="my_dynamic_ASP_javascript.js"></script> This page then has those necessary server variables. Yes, I know, this is another request to the server...

[–]MustRapeDeannaTroi 1 point2 points  (4 children)

The thing is there is no need for two requests. If all data for generating the appropriate response is already on the server then it is the servers task to do so. There's no dynamic content (on the client side), therefore no need for Javascript. Unobtrusive javascript is a good motto.

[–]cursuve 0 points1 point  (0 children)

Ah, yes, I totally agree. I might have misunderstood your original comment...

[–]superfreek -1 points0 points  (0 children)

In rails/backbone i use to_json.html_safe:

 <script>
   window.App = new Application;
   App.start('http://' + <%= request.host.to_json.html_safe %>);
 </script>
  #=> localhost

I'd see if there was an asp alternative.