Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hostname script

Status
Not open for further replies.

fredfredfred

IS-IT--Management
Aug 30, 2002
2
GB
Hi - complete novice here. I've been let down by my IT department and have to find a solution myself. Unfortunatly I know absolutly nothing about Javascript.

I need to add some script to a page that will run a particular script depending on the hostname. The basic functionality will be:

If hostname = " then run script1.js
If hostname = " then run script2.js
Else run script3.js

I don't know where to start so if anyone can help me out of this hole then I would be very greatful.

As I said I know nothing about javascript so please be gentle with me!

Cheers

Fred
 
How about something like this:
Code:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
var scriptName = '';
if (location.hostname=='[URL unfurl="true"]www.domain1.com')[/URL] scriptName='script1.js';
if (location.hostname=='[URL unfurl="true"]www.domain2.com')[/URL] scriptName='script2.js';
if (location.hostname=='[URL unfurl="true"]www.domain3.com')[/URL] scriptName='script3.js';
if (scriptName != '') {
  document.write('</scrip'+'t>');
  document.write('<scrip'+'t type="text/javascript" ');
  document.write('src="' + scriptName + '"></scrip'+'t>');  
  document.write('<scrip'+'t type="text/javascript">');
}
</script>
</head>
<body>
...
</body>
</html>
Hope that starts the ball rolling...

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Or like this?
[tt]
<head>
<script id="scriptid" src=""></script>
<script language="javascript">
window.onload=addsrc;
function addsrc() {
switch (window.location.hostname) {
case " document.getElementById("scriptid").src="script1.js";
break;
case " document.getElementById("scriptid").src="script2.js";
break;
default:
document.getElementById("scriptid").src="script3.js";
break;
}
}
</script>
</head>
[/tt]
 
Thanks both of you - I'll try these suggestions and see how much of a mess I can get into :)

Thanks for the quick resposes.

Cheers

Fred
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top