Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How do I create a custom bookmark that grabs the current URL?

I've seen a number of different sites where you can drag a link onto your bookmark bar in a browser and then have that bookmark act as a tiny little program that grabs and sends specific information. I want one like that for a bookmark program of my own, where I have a CGI backend and want to be able to visit a page, click on my link, and have the URL of that page handed to my script. Does that make sense?


Dave's Answer:

Your question is a bit confusing, yes, but I know what you mean because a few months ago I wrote a similar sort of utility that let me disseminate my favorite sites into an archive system I wrote on my server (yeah, kinda geeky, but, in the immortal words of Popeye, I yam what I yam. :-)

Anyway, here's the Javascript code I wrote for the bookmark itself:

javascript:location.href='http://www.mydomain.com/mybookmarkutility.cgi?u='+encodeURIComponent(location.href)+'&t='+encodeURIComponent(document.title);

As you can see, it's all a little Javascript trick here, where I'm accessing various elements of the document object model (the "DOM" for insiders), in this case the URL of the current page (that's location.href) and the title of the page too (that's document.title). You can't just slap those onto a new URL, however, so we also use a handy Javascript element called encodeURIComponent to turn spaces into "+", mask control characters and punctuation, etc.

If I was on a page with the URL, say, of "http://www.mypage.com/" and a title of "Interesting Information", then the resultant call to my CGI script would be:

?u=http://www.mypage.com/&t=Interesting+Information

The CGI script then simply needs to unwrap and identify the "u" and "t" variables to extract the URL and title again within the script. This should be easy to accomplish depending on your CGI programming skills.

For your purposes, you probably don't need the title of the page, so your task would be even easier: just delete the "t=" title portion.

In terms of how to get a bookmark set for your complicated Javascript code, the bst solution is to do something like this:

<a href="add javascript code here">Drag this bookmark onto your toolbar</a>

Which would be implemented thusly:

Drag this bookmark onto your toolbar (or, if you prefer, right-click and choose "Bookmark this link" or similar)

That should get you going with your project. Good luck!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments
Rather amazingly, there are no comments on this article yet.

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.