View Full Version : Make favorites javascript dynamic?

03-03-2007, 05:03 AM
1) Script Title: Bookmark site script

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex9/addbook.htm

3) Describe problem: Not exactly a problem; more like a desired capability.

What I'd like to be able to use is this bookmark script, but modified so that it grabs the page title and url automatically from each page it is on.

I use php to generate the html pages in my website, reading from one php include file; what would be great is if this bookmark script could grab the page url and title from the page it is on when it is clicked. That way, the script can be included without hardcoding of the title and url for each page.

Can javascript read page title and url's? Is this possible?

I figured out how to include the url in the javascript function with php, but it is complicated to get a file with php and parse the title info.

Any ideas will be good - I'm just learning coding...

03-03-2007, 05:27 PM
You already received your answer:


But, the page must be live. Just in case there was another error in that thread, here is my (slightly less sloppy) version of the same thing - Remember only Opera will add a local bookmark with these scripts, FF and IE require a live page:

Demo (http://home.comcast.net/~jscheuer1/side/bookmark.htm)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<title>Test Bookmark</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

* Bookmark site script- Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code

/* Modified to support Opera */
function bookmarksite(){
var title=document.title, url=window.location.href;
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
else if(document.all)// ie
window.external.AddFavorite(url, title);
<a href="javascript:bookmarksite()">Bookmark this site!</a>

03-03-2007, 07:38 PM
Yes, trying the test page as a simple html document was the problem. The script works fine on my webserver or locally under apache and OSX.

Now I'm searching for a way to support Safari with the script. What I've found are (I think) the functions for the bookmark dialogue for Safari:


and I think I'll be back for more help after I try to get this to work in another else/if statement....


03-03-2007, 08:04 PM
I seriously doubt that those commands, in and of themselves, will create a bookmark in any browser. But, I could be wrong. Before we go figuring out a test as to when to use them, make sure they work in Safari at all first.