9

I render a page using YUI. and depending on the user I need to change how it is rendered. This change is not something that can be parametrized, it is drastic and different for each user.

Please tell me how can I generate Javascript dynamically?

Ric Tokyo
  • 6,577
  • 3
  • 30
  • 48
Eastern Monk
  • 6,395
  • 8
  • 46
  • 61

4 Answers4

13

I personally use a PHP file to pass a JavaScript object made up of some basic session and internal settings, nothing mission-critical as passing information to the client isn't overly secure, but I believe it might follow the same principles as what you are looking for.

Similarly, I use this to display certain elements once the client is logged in, although all the authorization is still done on the server-side. If my session handler gives the PHP file the ok, it outputs a JavaScript object using a PHP heredoc string, otherwise, it doesn't output anything. You can use attributes of this object to compare against, or you could output only the JavaScript for how a certain page should be rendered, based on settings in your PHP file.

HTML:

<script src="common/javascript/php_feeder.php" type="text/javascript"></script>

PHP:

//my session handler authorisation check has been removed
//although you could place your own up here.

//assuming session was authorised
//set content type header
header("content-type: application/x-javascript"); 

$js_object = <<<EOT
var my_object = {
    my_attr: '{$my_attr}',
    my_attr2: '{$my_arrt2}',
    etc: '{$etc}'
}
EOT;

print($js_object);
Alex
  • 1,457
  • 1
  • 13
  • 26
Asciant
  • 2,130
  • 1
  • 15
  • 26
2

You can probably create two separate Java script files, and include the required file, depending upon the user type.

Pseudocode

If user_type is One
    <Script src='one.js' type='javascript'></script>
else
    <Script src='other.js' type='javascript'></script>
End If
M.N
  • 10,899
  • 13
  • 47
  • 49
0

JavaScript has an eval function, so I think (I haven't tried it) that you can generate JavaScript by writing it into a string variable (and then calling eval on that string variable).

ChrisW
  • 54,973
  • 13
  • 116
  • 224
0

A little bit of elaboration here would most certainly help in getting you a more descript and helpful answer. That in mind, though, you could easily just use functions declared inside an if statement to provide distinctly varied experiences for different users.

A very basic example:

<script>
function do_something(userType)
 {
if (userType == 'A')
 {
// everything you need to do for userType A
 }
if (userType == 'B')
 {
// everything you need to do for userType B
 }
 }
</script>
Hexagon Theory
  • 43,627
  • 5
  • 26
  • 30
  • 1
    I'd say, checking for user type should be a server-side thing instead of using a client-side javascript. (For obvious vulnerabilities) – M.N Feb 10 '09 at 08:37
  • Depends entirely on how he's gathering the data and whether or not he's competent in a server-side language, though; he could easily use the language he knows (JavaScript, it seems), and store the user's type in a JS variable; granted, yes, it would be beneficial to obtain this variable server-side. – Hexagon Theory Feb 10 '09 at 09:31
  • This is a very inefficient and insecure solution in my opinion. Also how exactly am I going to make a call to this function? – Eastern Monk Feb 16 '09 at 14:41