Bookmarklet To Trim Google Search URL

Matt Cutts, Google’s head of the webspam team, answered Which search feature would you add to Google? in the following Google Webmaster Central video.

In it he said he’d like to make the URLs cleaner, leaving just the search?q=whatever parts. He mentioned writing a Greasemonkey script, so I thought I’d write a little bookmarklet to help him out.

Just drag the following link to your bookmark bar and when you’ve completed a search in Google it will open a new window with the corrected search string and replace the URL.

javascript:(function(){var;var domainName=window.location.hostname;var myPathname=window.location.pathname;var searchArray=queryString.split('\&');var mySearch='';for(myElement in searchArray){if(searchArray[myElement].match(/^q=/)){var mySearch = searchArray[myElement];}}var searchOnly='http://'+domainName+myPathname+'?'+mySearch;with({self.location.href=searchOnly;document.close();}})()

Trim Google URL

Edit – Thanks to a comment by Bruno Sabot I’ve updated the bookmarklet to work with iGoogle.

Edit 2 – I’ve changed the way I grab the query string and now the bookmarklet seems to work okay in most flavours of the main Google search.

Edit 3 – See Trim Google Search URL – Now with Added Hash! for a version which handles the form of the Google URL with (or without) hashes.

10 thoughts on “Bookmarklet To Trim Google Search URL

  1. I wonder how many people email the the URL of the Google search result rather than one of the results?

    I think it’s a trend more prevalent with SEO’s rather than the mainstream public. I could be wrong.

  2. This bookmarklet do not work when you have this kind of URL (q= appear 3 times) :

    This one seems better :
    javascript:(function(){var windowLocationHref=window.location.href;var domainName=window.location.hostname;var myPathname=window.location.pathname;var searchArray=windowLocationHref.split(‘&’);var mySearch=”;for(myElement in searchArray){if(searchArray[myElement].match(/^q=/)){mySearch=searchArray[myElement];}}var searchOnly=domainName+myPathname+’?’+mySearch;self.location.href=’http://’+searchOnly;})();

  3. This just breaks for me on the “q”s, though I tend to use the Google Global Plugin by Redfly more than anything else to clean up searches, so I can grab a local result with personalization off.

  4. I find that I need to email Google search results a few times a month, and not for any SEO reasons. Usually when I do this I’m offering alternatives to people who are looking for a service or a product and are not familiar enough with Google to build the best search query, but I do it for other reasons too. While the format of the URLs never really bothered me, I can see why they would if you dealt with people internationally a lot. Good work.

  5. Andy, could you email a search that breaks and I’ll fix the bookmarklet. The Redfly plugin is great but I spend more time in Chrome now and so developed some bookmarklets for similar functionality.

  6. Something happened so that on safari (on my mac) the URL address field and google search field are not showing. I cannot type anything if i want to get to a different website, etc. I can only use my bookmarks.

Comments are closed.