• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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?
0
arantius
Asked:
arantius
  • 10
  • 3
  • 2
  • +1
1 Solution
 
VirusMinusCommented:
try swapping document.location with top.location
0
 
GwynforWebCommented:
l=(''+top.location).match(/[^/]+$/)[0]
0
 
arantiusAuthor Commented:
@gwyn
That doesn't execute in firefox, and in IE it only returns the filename

@Virus
Yeah, that does save 5 bytes =)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
GwynforWebCommented:
what are you trying to extract from the location string?  (it looked the file name to me)
0
 
GwynforWebCommented:
0
 
GwynforWebCommented:
in which case it is

l=(''+location.host).match(/[^.]*\.[^.]*$/)[0]
0
 
arantiusAuthor Commented:
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.
0
 
GwynforWebCommented:
in which case it is the above eg

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

javascript:function x(){document.getElementsByTagName('textarea')[0].value+='Thanks for the points';document.answerQuestionForm.submit()}x()
0
 
GwynforWebCommented:
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()
0
 
devicCommented:
Gwyn, I use always oldlook and adress is :
http://oldlook.experts-exchange.com:8080/Web/Web_Languages/JavaScript/Q_21189116.html
now it works correct ;)
0
 
GwynforWebCommented:
:-)
0
 
GwynforWebCommented:
this also seems to work with the hostname

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

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 10
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now