views:

55

answers:

6

I recently ran into a situation where it made sense (at first at least) to have my server-side code not only "print" html, but also some of the javascript on the page (basically making the dynamic browser code dynamic itself). I'm just wondering if there is sometimes a valid need for this or if this can usually be avoided...basically from a theoretical perspective.

Update:

Just to give an idea of what I'm doing. I have a js function which needs to have several (the number is determined by a server-side variable) statements which are generating DOM elements using jQuery. These elements are then being added to the page. So. I am using a server-side loop to loop over the number of elements in my object and inside of this loop (which also happens to be inside of a js function) I am aggregating all of these statements.

Oh and these dom elements are being retreived from an xhr (so the number of xhr requests is itself a server-side dependency) and then processed using jQuery..which helps explain why im not just printing the static html to begin with. Sounds kind of ridiculous I'm sure, but its a complicated UI..still I'm open to criticism.

+1  A: 

Yes there is sometimes a need and it is done often.

Here is a simple usage in an Asp.net like syntax

function SayHi( ){
  alert( "Hello <%= UserName %>");
}
BioBuckyBall
A little problem with this approach: what if you need to display the username at many different times, at many different places? I'd prefer `var username = <%= UserName %>;`. The point being: minimize generated code - make code easier to manage.
Here Be Wolves
I thought he meant constructing loops/control structures, i.e. sophisticated dynamically generated javascript.
aularon
@jrharshath - of course good programming practices are important. I'm just trying to illustrate a simple use case. What is this, reddit?
BioBuckyBall
@aularon I assume that too, but just wanted a simple illustration in addition to my original, too terse answer ( where I basically just said 'Yes' ).
BioBuckyBall
+1  A: 

I can smell some code smell here... if you're having to generate code, it probably means one of:

  1. you are generating code that does different things in different situations
  2. you are generating same kind of functionality, only on different things.

In case 1, your code is trying to do too much. Break your responsibilities into smaller pieces.

In case 2, your code is not generic enough.

Here Be Wolves
A: 

One usefull feature is to obfuscate your javascript on the fly.. When combined with a caching mechanism it might actually be useful. But in my opinion javascript generation should be avoided and all serverside variables should be handed to the script from the templates (on class or function init) or retrieved using XMLHTTP requests.

Johan
A: 

If your application generates javascript code only for data (eq. you want to use data from sql in js), the better practice is to use JSON.

sasa
A: 

Yes, sometimes for some specific task it may be easier to generate javascript on the fly in a bit complex way, like in rails rjs templates or jsonp responses.

However, with javascript libraries and approaches getting better and better, the need for this is becoming less, a simple example you may have needed to decide on a page whether to loop some elements and apply something or hide another single element, with jquery and other libraries, or even one's own function that handles some complex situation, you can achieve this with a simple condition and a call.

aularon
+1  A: 

Just a suggestion. If your server-side is generating HTML/Javascript, then you're letting view-side logic creep into your server-side. This violates separation of concern if you're following an MVC-style architecture. Why not use a taglib (or something of that nature) or send JSON from the server-side?

Vivin Paliath
+1 for talking about mixing responsibilities of view and server
Here Be Wolves
by "server-side" I also meant the "View" (in MVC terms). The view is still dynamic in the sense that it dynamically renders the presentation (either through taglibs or direct <% %> code snippets). So bottom line..your answer doesn't really apply. Either way its a good rule to follow. thanks.
ikl