Link to home
Start Free TrialLog in
Avatar of arantius
arantius

asked on

Byte saving, string manipulation

I'm 65 bytes off of the 508 byte limit for a bookmarklet to work in IE.  I'm looking to extract the TLD of the current page (whatever.com, whatever.org, ...).  I currently have:

var r="://([^/]+)",l=document.location.href.match(r)[1].split(".");l=l[l.length-2]+"."+l[l.length-1];

This is one of the bloated parts of my script. Can anyone save me a few bytes?
Avatar of VirusMinus
VirusMinus
Flag of Australia image

try swapping document.location with top.location
l=(''+top.location).match(/[^/]+$/)[0]
Avatar of arantius
arantius

ASKER

@gwyn
That doesn't execute in firefox, and in IE it only returns the filename

@Virus
Yeah, that does save 5 bytes =)
what are you trying to extract from the location string?  (it looked the file name to me)
ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada 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
yes, exactly.  i want the "domain name" with any sub domains stripped.
ebay.com for: www.ebay.com, signin.ebay.com, scgi.ebay.com, ebay.com

Etc.
in which case it is the above eg

javascript:function x(){alert(l=(''+location.host).match(/[^.]*\.[^.]*$/)[0])}x()
Beautiful Gwyn =)  Don't think it can get much smaller than that !
Avatar of devic
and what's about port? e.g. :8080
Thanks for the points
Above submitted from Netscape using

javascript:function x(){document.getElementsByTagName('textarea')[0].value+='Thanks for the points';document.answerQuestionForm.submit()}x()
to answer Devic (port number was not in question code)

l=(''+location.hostname).match(/[^.]*\.[^.]*$/)[0]
eg
javascript:function x(){alert(l=(''+location.hostname).match(/[^.]*\.[^.]*$/)[0])}x()
Gwyn, I use always oldlook and adress is :
http://oldlook.experts-exchange.com/questions/21189116/Byte-saving-string-manipulation.html
now it works correct ;)
this also seems to work with the hostname

l=location.host.match(/[^.]*\.[^.]*$/)[0]

javascript:function x(){alert(l=location.host.match(/[^.]*\.[^.]*$/)[0])}x()