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

通过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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 香农解码的代码问题,无法输出解码结果
      • ¥15 内网同一网段设备和wifi隔离
      • ¥15 Python操作注册表
      • ¥45 入门级别的一段VUE前端拍照像后端发送请求的代码,帮排错
      • ¥15 anaconda打开spyder后一直闪退,不知道怎么办
      • ¥15 解决迷宫问题中无法运行的问题
      • ¥15 关于aspnetcore中使用mqttnet库的entire
      • ¥15 关于#python#的问题,请各位专家解答!
      • ¥100 关于远控软件的两个问题
      • ¥15 基于STM32的AD8232心电采集装置设计