I have been developing and testing with inline jQuery. I am using CodeIgniter and the below refers to my views.
I have <script="text/javascript">JQUERY CODE</script> at the top of my page. Some of my code is dynamic in that it uses PHP vars, e.g. when making an Ajax call, the URL is preceeded by <?php echo base_url(); ?>.
I have gotten the code working, but I want to put it in an external JS file for cleanliness. There seems to be no way I can think of to do this. Am I correct?
One aspect of my site is Google Maps. I use them all over the place. Again, parts of the code are dynamic, from PHP. Same question.
As a slight change, with the maps, I have many pages with many slightly different maps on them. Is there anyway to efficiently reuse map code, or am I going to have to have some duplicate code?
So are your customizations mostly just parameter replacements for things like google maps? If so, why not just have a function call that take a JSON object as a parameter. Have your backend serve some JSON data via an AJAX request, pass the data to the function, and you’re done. Less JS code to download, and a much cleaner setup.
To use this, do something like this:
Output JSON from your app by creating something like this:
Hope that helps!