douniuta4783 2011-12-24 17:12
浏览 254
已采纳

通过HTML文件将参数从服务器端传递到Javascript

How do I pass parameters from a HTML file to a external Javascript file if these parameters are provided to the HTML by the server side (Codeigniter controller)?

In other words, I have parameters that I want to pass from the serverside PHP/Codeigniter to the Javascript file.

PHP/Codeigniter Serverside Code

function view($id) {

    $data['id'] = $id;    // this is the variable I want to pass to Javascript

    $this->load->view('index', $data);

}

HTML

<html>
    <head>
        <script type="text/javascript" src="./js/targetfile.js"></script>
    </head>
    <body>
        <?php echo $id; ?> //this is how I can retrieve the variable from serverside
...

Javascript (targetfile.js)

var id = id_from_serverside;  // This is where I want the serverside $id to go

Additional Info:

The variable $id is grabbed off the url, so for http://www.domain.com/view/1234, serverside variable $id will be set the value 1234. This 1234 value will then have to be passed to the javascript file (which does an AJAX call back to the serverside to retrieve data from the database)

  • 写回答

3条回答 默认 最新

  • douao1959 2011-12-24 17:30
    关注

    You should be able to just do this:

    <script type="text/javascript>
    var id = <?php echo $id; ?> //this is how I can retrieve the variable from serverside
    </script>
    <script type="text/javascript" src="./js/targetfile.js"></script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?