Get cgi.server_name via javascript?

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Any ideas on how I can retreive cgi variables via javascript? I'm googling and finding conflicting or seemingly invaluable info.

I basically want to set a javascript variable equal to cgi.server_name

var hostName = 'cgi.server_name';

That's obviously not working. Ideas?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
If I recall correctly those variables are actually handed over to the CGI script/process by the server, so I don't think they're available client-side by default. However, you could read the variables on the server when you build the page, and write them into javascript variables or hidden input fields in the document for access client-side.
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
If I recall correctly those variables are actually handed over to the CGI script/process by the server, so I don't think they're available client-side by default. However, you could read the variables on the server when you build the page, and write them into javascript variables or hidden input fields in the document for access client-side.

That's actually what I'm doing now. I was outputting the server_name variable server side so that js sets the variable equal to the string.

My goal was to see if I could do that without using the server, as most of the rest of the script doesn't.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,585
4,495
75
It sounds like you might be looking for window.location.hostname
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Any ideas on how I can retreive cgi variables via javascript? I'm googling and finding conflicting or seemingly invaluable info.

I basically want to set a javascript variable equal to cgi.server_name

var hostName = 'cgi.server_name';

That's obviously not working. Ideas?

That should work. One issue that arises is that the order of javascript code execution is different amoung browsers.

For example if your variable declaration is locsl within the html page, but you check the variables contents in an external javascript file, you may run into issues as the external files javascript is executed before the variable is declared.

Can you post the html output of the page? Is all of the javascript local to the html page? and all of it within the same script tag?


I would recommend you ouput the values to a hidden form field.
<input type=hidden value="cgi.servername" />

Then execute your javascript code in an onload event so you know the html will be processed and the variable will be there.