PDA

View Full Version : Dynamic javascript favorite script?



bluedogranch
03-02-2007, 04:06 AM
I found this "add favorites" javascript at
http://www.dynamicdrive.com/dynamicindex9/addbook.htm

What I'm wondering is how to make it dynamic, i.e. get the page URL and page title for each page the script is on. I'd like to load the script (with the php print function) on each of my web pages, and have it get the page title and URL from each page so I don't have to code that in for each page.

I figured out how to get the page url into the html link with php, but using php to parse a page title is quite involved.

Anyone know how to do this all in javascript? Would you call variables like:
myURL = window.location.href and myTitle = document.title?

I'm still learning........

javascript:

function bookmarksite(title,url){
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}


The script is called on the webpage with this:

<a href="javascript:bookmarksite('Dynamic Drive', 'http://www.dynamicdrive.com')">Bookmark this site!</a>

codeexploiter
03-02-2007, 04:30 AM
I've changed the original script's function a bit which I colored in red (changes) in the following code



function bookmarksite(){

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');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}


Please note that the function doesn't have any arguments now so you can call it just like bookmarksite()

Example


<a href="javascript:bookmarksite();">Bookmark this site!</a>

bluedogranch
03-02-2007, 06:59 PM
For some reason, that doesn't want to work. I tried it in Firefox and IE, but no luck.

Do I need to somehow call the variables inside the () in the link?

<a href="javascript:bookmarksite();">Bookmark this site!</a>