views:

41

answers:

3

So i have been developing and testing with jquery inline. I am using CodeIgniter - 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();?>

So now I have gotten the code working, I want to put it in an external JS file for cleanliness - thinking about this, there is seemingly no way. 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?

Thanks

A: 

Well, do the same as you would do inside the page directly. Rename the .js file to a .php script, and include the same PHP code as you would before, in the .php file. Then, in the HTML file, use filename.php (not .js). The web server will invoke PHP on the script and send the browser the resulting JavaScript.

Delan Azabani
Sorry, I was not clear - Using codeigniter is the crucial bit here. I am passing data to my view files. This is data that has been gotten from a database for example. In my view i have my js code - to utilize the method of naming the js file .php, I'd have to overcome the passing of data to the file first.. As such am i stuck having it inline?
Thomas Clowes
Don't do this - you really should move towards a pure Javascript solution, as you seem to (correctly) desire already.
Pointy
+2  A: 

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.

// site.js
function displayMap(params) {
  // code to display google map using values from params
  // for instance:
  var lat = params.lat;
  var long = params.long;
}  

To use this, do something like this:

$.get('ajax/mapdata.json?id=5', function(data) {
  displayMap(data);
  alert('Load was performed.');
});

Output JSON from your app by creating something like this:

{
  lat: 232
  long: 123
}

Hope that helps!

Tauren
Tauren, Thanks for the response. This may very well be what I need- I however do not understand what you are saying. Could you possibly explain in idiot language? Thanks
Thomas Clowes
Basically i need to pass things such as the latitude and longitude i have gotten from a database, the base_url(); value, a username etc
Thomas Clowes
@Thomas two ways to do it: you can have an AJAX mechanism as @Tauren suggests, or your main php page can just dump all the parameter data into a JSON structure that your external pure Javascript finds and uses.
Pointy
@Thomas: So it sounds like you aren't really changing JS logic, just values. That's good, because in my opinion maintenance, versioning, downloadable size of JS, caching, and much more worsens going that route. For your needs, I'd highly recommend just returning a JSON object in your page or via an AJAX call, and having a function that takes that JSON and uses the values within it.
Tauren
Ok. I am going to have to research this - this is new to me, but is the essential idea that i can have an external js file which i call in my page, i can then encode all my php vars (as needed), and these are available to the already called js file? Thanks
Thomas Clowes
@Thomas. Just added some simple code to help illustrate. Look into jquery's $.ajax, $.get, $.post, $.load. The main idea is that you should have a STATIC javascript file. This makes it so that your clients can cache it. Code GENERIC functions in it, that take values as parameters, not hardcoded values. Pass those values to your app using an AJAX call to get JSON data, or by inlining the JSON data into your page.
Tauren
Hmm.. sorry for making this difficult, I am a bit of a novice. So.. Tauren, you are suggesting I have a static JS file, I get data through an inline ajax call using load, and then pass it to a static function in my external .js file? Is that correct? If so - it does not quite fit my situation - i have data already, lets say $a, $b, and $c. I am including the external js - on that basis is Pointys' suggestion to have a little inline js setting these php vars as js vars, then include the external JS? Which will have access to them? Thanks
Thomas Clowes
If you prefer to inline the data in your PHP page, then go for it. But it is much more common these days to use ajax calls to get data and javascript to render that data. Also, you would use get() or post(), not load(), as load() gets HTML data and replaces elements with it. It sounds like you need to spend a little time researching json and ajax, but you could certainly just inline the data and still have a static JS function call to render it.
Tauren
Yes, I most certainly do :) This is all relatively new - i simply feel i should do it right from the get go :) So the more common way would to be to simply load my view (pass no data), and then get the data using ajax? The most obvious flaw (seemingly) is that then to get any data YOU must have javascript enabled, whereas this way the data is still there and can be used for some sort of graceful degradation..?
Thomas Clowes
@Thomas: Don't you need JS to display google maps? And what do you do with the inline data if you don't have JS enabled? Display the lat/long in a table or something? I'm all for graceful degradation, but there is a point where they just won't get useful stuff if they don't have JS enabled.
Tauren
Oh, one more thing. I meant to use $.getJSON, not $.get().
Tauren
Well I was thinking, in php i output content to a div. If you have javascript enabled, some more exciting data replaces the content in the div. . . This somewhat links to a q i asked yesterday : http://stackoverflow.com/questions/3693244/jquery-load-and-seo-anyone-got-a-decent-answer
Thomas Clowes
A: 

You can create a block of JSON data in your main page:

<script>
  var pageData = {
    something: 'whatever',
    somethingElse: [ 33.20, 19.02 ],
    moreStuff: false
  };
</script>

and then just reference the variable "pageData" from your script.

Pointy
which is an external .js - it doesnt need to be named .php because using a little inline js i have set the js vars to the php values.. right? Thanks
Thomas Clowes
Right - it should be plain old Javascript in a ".js" file. The little block of Javascript on your page should just contain the dynamic data that the script needs.
Pointy