"Hidding" javascript when it's included in php?

Not sure where to post this, but ill try here.

When I add/include an clean javascript like this:

echo '<script type="text/javascript" src="js/ajax.js"></script>';

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:


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?

Thanks in advance…

If your setting custom vars in your javascript source that need to be set with PHP do so like this for example…

echo '<script type="text/javascript">
    var myVar = "'.$something.'";
    var new   = "'.$newThing.'";
echo '<script type="text/javascript" src="js/ajax.js"></script>';

This is how i do it when i need JS vars declared with PHP values and it for me personally is a lot easier then managing PHP files for JS.

But how do I then get the vars into ajax.js?

Declaring the vars before the JS file makes them part of the global scope there for in your ajax.js you can do something like

function ajaxConnection(){
    var connect_to = myVar;
    var newSet     = new;

NOTE: This is just an example, the method new is a reserved word and cannot be used.

Can’t get it to work…

<script type="text/javascript">
	var nlat = "48";
	var nlng = "53";
	var nzoom = "121";

Are then trying to get it like this:

function load() {
   var nlat = nlat;
   var nlng = nlng;
   var nzoom = nzoom;

But with no luck… The vars just says “nlat,nlng,nzoom”…

Where do I go wrong?

Try using var names in the [B]load/B function besides the global scope vars

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.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(nlat, nlng), nzoom);
	var zoomlevel = map.getZoom();

What to do…

Call load() when an event such as page load is triggered.

<body onload = “load(nlat, nlng, nzoom );”>

Why not use:

echo '<script type="text/javascript" src="js/ajax.[b]php[/b]"></script>'; 

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.