Link to home
Start Free TrialLog in
Avatar of Graeme McGilvray
Graeme McGilvrayFlag for Australia

asked on

identifying sub domains in webpage

Hey all, just wondering if there is a way to identify which subdomain is being used on the page, there fore displaying a specific image for it

eg.

sub1.domain.com.au > image of monkey

sub2.domain.com.au > image of tiger

the pages are identical, just need an if statement to identify the subdomain

Thanks in advance
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Something like this should work (javascript).
var wl = window.location.href;
	wl = wl.substr(0, wl.indexOf('.'));
	wl = wl.substr(wl.indexOf('/')+2);
	if(wl === 'sub1'){
		//display image of monkey
	}else if(wl === 'sub2'){
		//display image of tiger
	}

Open in new window

To get the sub domain  name server side you could use the following server variable:

url = Request.ServerVariables("SERVER_NAME")

then you can do an if statement on that variable and display the appropriate image
Avatar of Graeme McGilvray

ASKER

Big Monty, are you able to give an IF statement example?
Sure no problem, try the code below and let me know if you have any questions.

url = Request.ServerVariables("SERVER_NAME") 
If InStr( url, "sub1" ) > 0 then 
    Response.Write "<img  src=' monkey.png' />
elseif InStr( url, "sub2" ) > 0 then 
    Response.Write "<img  src=' tiger.png' />
end if

Open in new window


If you have multiple conditions you want to check, just add them in the IF statement.
Thanks for the BigMonty, it sends back the whole domain, not the just the sub (which is what i would like)

ideas?
it sends back the whole domain, not the just the sub (which is what i would like

not sure what you mean by this. the code just checks for any occurrence of the string "sub1" in the first if statement and for the string "sub2" in the 2nd if statement.

if you do a Response.Write URL, what do you get?
Hi BigMonty. I get the full domain. Eg

Sub1.maindomain.com

I just would like sub1 as the outcome
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks a treat! thank you very much!