Solved

Byte saving, string manipulation

Posted on 2004-10-30
348 Views
Last Modified: 2008-02-01
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
Question by:arantius
    16 Comments
     
    LVL 30

    Expert Comment

    by:VirusMinus
    try swapping document.location with top.location
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    l=(''+top.location).match(/[^/]+$/)[0]
    0
     
    LVL 18

    Author Comment

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

    @Virus
    Yeah, that does save 5 bytes =)
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    what are you trying to extract from the location string?  (it looked the file name to me)
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    0
     
    LVL 31

    Accepted Solution

    by:
    in which case it is

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

    Author Comment

    by:arantius
    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
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    in which case it is the above eg

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

    Author Comment

    by:arantius
    Beautiful Gwyn =)  Don't think it can get much smaller than that !
    0
     
    LVL 25

    Expert Comment

    by:devic
    and what's about port? e.g. :8080
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    Thanks for the points
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    Above submitted from Netscape using

    javascript:function x(){document.getElementsByTagName('textarea')[0].value+='Thanks for the points';document.answerQuestionForm.submit()}x()
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    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
     
    LVL 25

    Expert Comment

    by:devic
    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
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    :-)
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    this also seems to work with the hostname

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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    This article shows how to read a single webpage's HTML into a string variable, and it also shows how to automate a sequence so you can read and process a list of webpages.  I was tasked with reading and parsing a series of webpages to collect some s…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now