...

View Full Version : analysing current URL using RegExp?



ush
04-04-2003, 10:00 AM
Hello!

I have a custom 404 error page which handles errors from a number of web servers...

The main server is www.thegrid.org.uk.

The others are www.[schoolname].herts.sch.uk where [schoolname] is any one of 560+ schools hosted on our server.

What I'd like to do is analyse the current URL (using javascript with RegExp?) and use document.write on the 404 page to indicate to the user whether they have come from the main site or a school's site (since, frankly, most of them don't realise where they've come from) and give them different instructions on how to report the missing page

I have no server-side scripting with which to accomplish this, hence I'm resorting to javascript.

I've been supplied the attached .js file but it doesn't work - as a js novice I'm in need of some specialist help, hence I'm here. Is the attached file workable or is there a more elegant solution?

Anything you can help me with is greatly appreciated!

Cheers

ian.

chrismiceli
04-04-2003, 02:28 PM
<script type="text/javascript">
url = parent.location.href;
if(url="http://www.thgrid.org.uk") {
//code here
}
else {
//code here
}
</script>

Graeme Hackston
04-04-2003, 02:47 PM
Chris, a typo

if(url == "http://www.thgrid.org.uk") {

ush
04-04-2003, 03:07 PM
Thanks, but will this only match the URL if it's exactly www.thegrid.org.uk - for example will it match www.thegrid.org.uk/foo/bar.html ?

I guess I want something that matches the string if it contains thegrid.org.uk (there are other thegrid.org.uk servers without "www")

liorean
04-04-2003, 07:47 PM
function testURL(url){
// Do they have to use www.? If so use
var
reMain=/^http:\/\/www\.thegrid\.org\.uk/i,
// Otherwise use
reMain=/^http:\/\/(www\.)?thegrid\.org\.uk/i,
// Same goes for schools,
reSchool=/^http:\/\/www\.(\w+)\.herts\.org\.uk/i,
// or
reSchool=/^http:\/\/(www\.)?(\w+)\.herts\.org\.uk/i,
sSchool;
if(reMain.test(url)){
// do things if from Main site here
}else if((sSchool=reSchool.exec(url))){
// do things if from School site here
// sSchool.pop() is the school name, if you want it.
};
return anythingYouLike;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum