It looks nice and needy in my source, but sometimes I need to put some PHP inside the javascript and I then need to make my javescript PHP and include the script like this:
include_once("js/ajax.php");
This makes it viewable in my source and doesnt look nice… Is there another way to do this so the whole PHP javascript doesnt get into my source?
Nomatter how I do this it won’t work… In my source it only shows up as nlat, nlng, nzoom… I want it to show the numbers…
Here is the script…
var nlat = 56.26392;
var nlng = 10.501785;
var nzoom = 6;
function load(nlat,nlng,nzoom) {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setMapType(G_HYBRID_MAP);
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(nlat, nlng), nzoom);
var zoomlevel = map.getZoom();
}
}
That way the file containiing the JavaScript gets processed for any PHP it contains on the server before it is passed to the browser for processing as JavaScript.
Browser caching. Even if no-cache headers are sent, even with ‘must-revalidate’ flags in place I wouldn’t be surprised to find at least a few browsers ignoring them for the life of the session because this is a support file and not the main page file. I could be wrong but I’d test for the possibility.
My suggestion is to use a X-JSON header to hold the values. Those aren’t just for ajax responses - they can be embedded into a page feed and read by the javascript of the page.
I have never had that problem but then the PHP in the file should only need to be run the once prior to the page first loading. If you want to run more PHP after the page loads then an ajax call from within the JavaScript is appropriate.
The only difference between having the JavaScript embedded inside the HTML of a PHP page and what I suggested is that one is jumpled inside the HTML and the other is not. In both cases all the PHP runs before the files are sent to the browser.