Results 1 to 3 of 3

Thread: Dynamic javascript favorite script?

  1. #1
    Join Date
    Mar 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Dynamic javascript favorite script?

    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>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I've changed the original script's function a bit which I colored in red (changes) in the following code

    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

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

  3. #3
    Join Date
    Mar 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •