Forum Moderators: open

Message Too Old, No Replies

Getting PHP variable in separate .JS file

         

csdude55

5:52 pm on Feb 8, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm pretty sure the answer is "it can't be done", but if you don't ask you don't know! LOL

I set a handful of variables in variables.php script that's included on every page of the site. Examples are the absolute path to images, the basepath, the size of the navigation array, and Adsense IDs. These rarely change, but since I have a ton of parked domains they're different for each of those domains.

To use them in Javascript, I have to have a <script>var foo = '$bar'; ...</script> section in my header.php. And afterward, I have a separate <script src="javascript.js"> to get the rest of my Javascript functions.

So the question is, can you guys suggest a way to get the PHP variables inside of javascript.js, without hard coding them or loading them in header.php first?

If it matters, I'm loading jQuery so I can use jQuery suggestions.

lammert

6:13 pm on Feb 8, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The jQuery.getJSON() function would be a good start. It loads JSON data from a remote URL. Your variables.php script should generate valid JSON data for this to work.

robzilla

6:55 pm on Feb 8, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It may be a little unconventional, but if you insist on including them in javascript.js you could have a PHP script replace (or: act as) the static file javascript.js via a rewrite. So you'd put the contents of javascript.js in, for example, javascript-js.php and include some code that customizes the variables, then configure your web server to serve the PHP file whenever a client requests javascript.js (with the appropriate headers).

To minimize the negative effects of replacing a static file with a dynamic one, you could employ some sort of caching mechanism, e.g. based on the domain name or whatever you like.

csdude55

4:22 pm on Feb 10, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the ideas! I'd never seen jQuery.getJSON() so that seems like the simplest plan, but I also think it might be another DOM query... so marginally slower.

I'm kind of digging @robzilla's idea... I use $_SESSION to store most of the variables, anyway, so it's just the initial run that would be slower if I do that. I'm gonna play around with both options and see which one is faster to process. Thanks again!

NickMNS

4:59 pm on Feb 10, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My first question is why? The method you are using seems to me to make the most sense. The data is relevant to the page, so passing it to the page with PHP makes sense. Embedding it in the JS, as you would like and as proposed by Robzilla will work, but the js script can be used on any number of pages. The result is that now you will have a unique script for each page as each script will include the data that is specific to the page that is calling it. This can have unintended consequences, for example caching will be far less effective as there will now be as many js files as there are pages, as opposed to a single js file that can be cached once and served to many pages. It seems to me to be less than ideal.

...Query.getJSON() so that seems like the simplest plan, but I also think it might be another DOM query... so marginally slower.

It does not require another DOM query, it requires another request to the server. getJSON() is syntatic sugar for an AJAX request that waits for a JSON response.
[api.jquery.com...]

This is another way of getting the data, and it works, and moreover it keeps the data separate from the script (avoiding the caching issues), but it comes at the cost of one or more requests to the server.