www.webdeveloper.com
Results 1 to 4 of 4

Thread: problem creating bookmark link in Opera, Chrome, and Safari

  1. #1
    Join Date
    Apr 2005
    Posts
    35

    problem creating bookmark link in Opera, Chrome, and Safari

    Hello,

    I've searched high and low for some decent code that will allow a visitor to a website to bookmark the site through a hyperlink. The bookmarking is done through a javascript function that looks like this:

    Code:
    function OnBookmarkButtonClick (e)
    {
        if (window.sidebar)
        { // Mozilla Firefox Bookmark
    	    window.sidebar.addPanel("TimeSheet", window.document.location,"");	
    	}
    	else if (window.external)
    	{ // IE Favorite	
    	    window.external.AddFavorite( window.document.location, "TimeSheet");
    	}
    }
    I need to add some detection for Opera, Safari, and Chrome. Please note that it must be feature detection, not browser detection. But as I said, I've searched high and low for this, and couldn't find anything.

    Help is much appreciated.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    For Opera:
    Code:
     function CreateBookmarkLink(alink) {
    
     var title = "GOOGLE"; 
     var url = "http://www.google.com";
    
    
    	if (window.sidebar) { // Mozilla Firefox Bookmark
    		window.sidebar.addPanel(title,url,"");
    	} else if( window.external ) { // IE Favorite
    		window.external.AddFavorite(url,title); }
    	else if(window.opera && window.print) { // Opera Hotlist
    		alink.href=url;
    		return true; }
     }
    
    //
    
     if (window.external) {
      document.write('<a href = "javascript:CreateBookmarkLink(this)");">Add to Favorites</a>'); 
      } else  if (window.sidebar) {
      document.write('<a href ="javascript:CreateBookmarkLink(this)");">Bookmark Page</a>'); 
     } else if (window.opera && window.print) {	
       document.write('<a rel="sidebar" href = "javascript:CreateBookmarkLink(this)");">Add Bookmark</a>');
     }
    For Chrome and Safari there is no JavaScript solution. Simply those browsers do not permit the Bookmark handling via JavaScript. You may, eventually, detect Chrome and Safari and launch an alert, something like:
    Code:
    else if(window.chrome){
    alert('Press ctrl+D to bookmark (Command+D for macs) after you click Ok');
    }
    Last edited by Kor; 11-24-2011 at 03:29 AM.

  3. #3
    Join Date
    Apr 2005
    Posts
    35
    Is testing for window.opera considered browser detection? This is what I'm trying to avoid.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by gibby View Post
    Is testing for window.opera considered browser detection? This is what I'm trying to avoid.
    Sir, no detection of the browser type is 100% reliable, because browsers can be spoofed. The best way to detect a browser is detecting a specific method, but I don't think Opera has such of method

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles