0

I have a Switches.js javascript file, like this:

//GLOBAL switches
//Change the values accordingly

var SWITCHES = {
    uber:        true, //Uber
    social:      true, //Social media pulgins
    g_charts:    true, //Google Charts
    g_captcha:   true, //Google Captcha
    g_analytics: true, //Google Analytics
    data_base:   true, //Inserts user input data into DataBase
    print:       true, //Print option
    pdf:         true, //Download PDF report option
    https:       true  //true for https, false for http
};

This JS file is stored in my server.

Is there a way to get that object and one of those properties into a php variable? I think that should be around the php functions file_get_contents and json_decode, but how do I take into account the comments in the JS file and that variable SWITCHES in particular?

$file_content = file_get_contents("Switches.js");
//some stuff
$http_switch = $SWITCHES[https];

AJAX nor jQuery are an option because this is to be run purely server side.

5
  • It's JavaScript, not JSON, so you can't use json_decode(). How about converting the Switches.js to Switches.json instead? Commented Sep 2, 2017 at 21:56
  • @Svenskunganka , thank you for the reply. And then how could I use such JS SWICTHES variable in my global JS scope? Commented Sep 2, 2017 at 21:57
  • You'd use the equivalent JavaScript function JSON.decode() to turn it into a JavaScript Object. Commented Sep 2, 2017 at 21:59
  • seems interesting your suggestion and it solves my problem. Could you reply accordingly with detailed explanation being then tagged as solved? :) Commented Sep 2, 2017 at 22:01
  • There we go, btw I wrote a typo in one of my previous comments, JSON.decode() is meant to be JSON.parse(). Commented Sep 2, 2017 at 22:08

1 Answer 1

2

Since the Switches.js is JavaScript and not JSON, you can't use json_decode(). Instead you should convert it to JSON and decode/parse it both client-side and server-side:

In PHP, you'd do:

$data = file_get_contents("Switches.json");
$switches = json_decode($data);

And then client-side you fetch it via AJAX and parse:

$.get("/Switches.json", function (data) {
  var switches = JSON.parse(data)
})

Your Switches.json file would then be (JSON files must not have comments):

{
    "uber": true,
    "social": true,
    "g_charts": true,
    "g_captcha": true,
    "g_analytics": true,
    "data_base": true,
    "print": true,
    "pdf": true,
    "https": true
}
Sign up to request clarification or add additional context in comments.

3 Comments

is it possible to fetch it synchronously without AJAX? I'll need those variables right away for all the other JS files.
Yeah with var switches = JSON.parse($.get("/Switches.json")). However synchronous requests are deprecated so it is adviced not to use that. From MDN docs: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), synchronous requests on the main thread have been deprecated due to the negative effects to the user experience.
all my JS files are loaded at the end right before the </body>, so there is no issue with user experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.