将JS集成到JS中的干净方法
Clean way to integrate JS into JS
我的目标是从PHP生成一些JS。这是我script.php的内容
这样我可以稍后使用
<script src="script.php?id=1&username=tom"></script>
这是script.php 的内容
if (isset($_REQUEST["username"])) {
$username=$_REQUEST["username"];
$params.="&username=$username";
}
if (isset($_REQUEST["photo"])) {
$photo=$_REQUEST["photo"];
$params.="&photo=$photo";
}
?>
document.write('<link href="chat.css" rel="stylesheet" type="text/css" />');
window.jQuery || document.write('<script src="http://code.jquery.com/jquery-1.10.1.min.js"><'/script>');
document.write('<script src="chat.js.php<?=$params?>"></script>');
document.write('<div id="chatListUsers"></div>');
document.write('<div id="chatContainer"></div>');
有没有办法让它比:document.write("….")
一种模板系统?
document.write
是向页面动态添加script
标记的好方法,但更好的方法是使用这样的脚本加载程序。
但是,看起来你不想动态添加script
,看起来你想用动态参数调用脚本,在这种情况下,你可以完全跳过document.write
,只使用纯HTML,就像这样:
<script src="chat.js.php<?= $params ?>"></script>
相关文章:
- 当用户点击动态创建的链接时,如何调用JS方法.JQuery,ASP.NET MVC
- JS方法显示了未定义的简单示例
- 如何从方法内部获取js方法名称
- 如何在Spotify应用程序中创建discincts JS方法
- 正在寻找特定的JS方法来确认单选按钮的选择
- jquery在上下文中调用JS方法
- js方法链尾
- JS方法绑定与2个不同的这个
- Wy js 方法 toFixed() 返回 NaN
- 从链接调用 js 方法
- 正确的方法是什么,使用THREE.js方法来计算相机的偏航
- 为什么 findRandom() mongoose for node.js 方法不起作用
- Node.js 方法不返回任何响应,挂起请求
- 缺点和替代方案 - JS方法链接
- Rhino API-使用org.mozilla.javascript.Context访问js方法
- jQuery/JS方法链接
- 通过ajax调用从js方法返回值到REST服务
- 在JS方法asp.net C#中获取变量的内容
- 从复杂对象中提取选定数组和属性的JS方法/lib
- UIWebView elementFromPoint js方法return'错误'要素