I am trying to pass my variables from my main page to external JS file. Please see my code.
Main page:
<script type="text/javaScript">
$(document).ready(function(){
var add_project_link="<?= base_url().'add_project/show_form'; ?>";
var add_agent_link="<?= base_url().'agent/add_agent';?>";
});
</script>
External JS
$(document).ready(function(){
alert(add_project_link);
alert(add_agent_link);
});
I got:
uncaught referenceError add_project_link is not defined.
I thought the external JS would catch the variable declared in main page. Any thoughts? Thanks a lot!
Before you call your external JavaScript file, define your variables using PHP:
In your external file.js you can now use
site_urlas a variable.