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)