Friday, June 13, 2008

Bookmarking with Flash and Javascript

Bookmarking using Flash

To bookmark a page from a Flash call, flash needs to call an external JavaScript function in your page to actually do the bookmarking any browser.

But here is the trick....

In order for the bookmarking prompt window for example in IE to show up the focus needs to be outside the flash object. So you need to set a focus to a DOM element outside flash.

Here is an example script that works.
function bookMarkPage(url, title) {
// set focus to a DOM element, otherwise it won't work.
$('flash_container').focus();

switch(BrowserDetect.browser){
case "Firefox":
window.location.hash = url;//set the hash value
alert("Please press 'Ctrl + D' to bookmark this page.");
break;

case "Explorer":
window.external.AddFavorite(url, title);
break;

case "Safari":
alert("Please press 'Ctrl + D' to bookmark this page.");
break;

case "Opera":
// Create document element dynamic and invoke
var bookMark = document.createElement('a');
bookMark.setAttribute('rel','sidebar');
bookMark.setAttribute('href',url);
bookMark.setAttribute('title',title);
bookMark.click();
break;

default:
alert("Please bookmark this page manually.");
break;
}
}


No comments:

Post a Comment