Closed Thread Icon

Topic awaiting preservation: get domain from url string? Pages that link to <a href="https://ozoneasylum.com/backlink?for=8910" title="Pages that link to Topic awaiting preservation: get domain from url string?" rel="nofollow" >Topic awaiting preservation: get domain from url string?\

 
Author Thread
smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 10-15-2003 14:24

Hi,

Ok I'm sure this is simple, maybe javascript evan has a built ability to do it, but I can't find things on google coz I'm quite sure what to ask.

Basically I need to get the domain/url without all the subdirectories and stuff on the end.

So for instance if the current page was http://www.ozoneasylum.com/cgi-bin/postings.cgi?action=newtopic&nu... the JS would return http://www.ozoneasylum.com

Is there a 'getDomain' type function in JS, or is it a case of getting the whole long url string for the current page and using some kind of regex to match and remove the chaff on the end?

Any help would be appreciated.

Thanks guys

trib
Paranoid (IV) Inmate

From: Den Haag, Netherlands
Insane since: Sep 2002

posted posted 10-15-2003 16:27

Use split() ... google search for "javascript split" found this ...
http://hotwired.lycos.com/webmonkey/98/29/index1a_page4.html?tw=programming

I think it's what you wanted.

[This message has been edited by trib (edited 10-15-2003).]

MajorFracas
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2003

posted posted 10-15-2003 18:01

Try location.host or location.hostname instead. (location.host includes domain & port; location.hostname is just the domain...)

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 10-15-2003 22:30

Thanks guys, works like a dream:

final code:

code:
function exturl() {
// var copyRight="Jon Burger - [url=http://www.cryokinesis.co.uk"]www.cryokinesis.co.uk"[/url]
var isOp = (navigator.userAgent.toLowerCase().indexOf('opera')!=-1)?true:false;
var dL=document.getElementsByTagName('a')
for(i=dL.length;i!=0;i--){
var a=dL[i-1]
if(!a.href.indexOf('http:\/\/') && a.href.indexOf('http:\/\/'+location.hostname)){
a.onclick=function(){
var nuWin=window.open(this.href);
if(nuWin && !isOp)nuWin.focus();
return (nuWin)?false:true;
}
}
}
}
wE=window.addEventListener;wA=window.attachEvent;
wE?wE("load",exturl,false):wA?wA("onload",exturl):null;



Basically I got sick of having to add xhtml compliant 'open in new window' code to all the links to external pages on my site so I created this. It was written by me then I updated the actual window opening function to only return false if the new window has opened, if not it returns true and the link acts normally. That method:

var nuWin=window.open(this.href);
if(nuWin && !isOp)nuWin.focus();
return (nuWin)?false:true;

was partly from a concept by some other guy. Can't remember who tho - what is the copyright issues with that? he didn't have a notice in his code tho - it was just a web tutorial/article thing. I'm not worried about how many people use my code which is very simple anyhow, I'd just like to keep my name on it for pure ego reasons

If anybody can see any possible issues with this code other than lack of older browser support then let me kno.

visit my CryoKinesis Online Gallery

« BackwardsOnwards »

Show Forum Drop Down Menu