如何生成动态Javascript? [英] How can I generate Dynamic Javascript?

查看:91
本文介绍了如何生成动态Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用YUI渲染页面。并且根据用户的不同,我需要更改它的呈现方式。这种变化不是可以参数化的,对于每个用户来说都是极其不同的。

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.

请告诉我如何动态生成Javascript?

Please tell me how can I generate Javascript dynamically?

推荐答案

我个人使用php文件来传递由一些基本会话和内部设置组成的JavaScript对象,没有任何关键任务,因为将信息传递给客户端过于安全,但我相信它可能遵循与您所寻找的相同的原则。

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.

同样,我会使用它来显示客户端登录后的某些元素虽然所有授权仍在服务器端完成。如果我的会话处理程序给PHP文件ok,它会使用PHP heredoc字符串输出一个JavaScript对象,否则它不会输出任何内容。您可以使用此对象的属性进行比较,或者您可以根据php文件中的设置仅输出特定页面的呈现方式。

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);

这篇关于如何生成动态Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆