Free tech support / small logo


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!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments
Rather amazingly, there are no comments on this article yet.

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

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











Remember personal info?


Please note that I will never send you any unsolicited 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.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 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.