Global keyword in Javascript


(Serge) #1

Matrix Version:

Any reason why this doesn’t work within a page container?

<script>
    alert("I am on the host " + "%globals_server_HTTP_HOST%");
</script>

(Mahearnpad) #2

What’s not working?

The keyword is a PHP global. You can’t use it in JavaScript. In any case, you would need to remove the quotes from around the variable name for it to work.

If your container is a “raw_html” type, then you should see the alert at least.


(Bart Banda) #3

Yea, make sure the content container is set to Raw HTML. That exact code should work fine if you are just trying to alert a string such as “I am the host www.example.com


(Serge) #4

Doesn’t PHP process the page before sending the output to the browser and then the browser execute the Javascript? I’ve been coding in php professionally for 17 years, I’m yet to see a that working the other way around :wink:

Yes it is in raw html:

And I get

Which indicates it get substituted by “” otherwise I’d get the string %globals_server_HTTP_HOST% displayed. Is there a reason why $_SERVER['HTTP_HOST'] would be missing / empty?


(Mahearnpad) #5

What do you get if you use console.log in Chrome or Firefox? That will at least give you any errors so you can pin down the problem.


(Serge) #6

The problem is at the Server / PHP level. Either the macro %globals_server_HTTP_HOST% isn’t recognised or $_SERVER['HTTP_HOST'] is empty.

Raw HTML/JS content received from the server shows:


(Bart Banda) #7

Yea might be due to a configuration on your web server.

What happens if you try some other global server vars, such as %globals_server_REQUEST_TIME% ?


(Serge) #8

Same result for %globals_server_REQUEST_TIME%, and others; empty. I’ve also tried outside the <script> tags just in case the substitution excluded content within those.


(Bart Banda) #9

I think it might be something with the server setup then. I’d send a support ticket in to Squiz to see if there is a simple config that they can change to enable it (if your site is hosted with Squiz that is).