使用JavaScript添加浏览器书签 [英] Adding Browser Bookmarks using JavaScript
问题描述
我有一个带有按钮的ASP.NET网页.单击该按钮,应将书签保存在浏览器中,并且当用户单击书签时,应浏览到 http://google. com .
I have an ASP.NET web page having a button in it. Clicking the button, a bookmark should be saved in the browser and when the user clicks the bookmark, it should surf to http://google.com.
我如何确保它可以与几乎所有标准浏览器兼容,或者至少与IE,Mozilla Firefox,Opera和Google Chrome浏览器兼容.
How do I make sure that it works with almost all the standard browsers or at least with IE, Mozilla Firefox, Opera and Google Chrome.
在另一种情况下,我也以相同的方式创建了第二个书签.但是,当用户单击第二个书签时,它应该运行一段JavaScript代码.
Another case, I create a 2nd bookmark also in the same way. But when the user clicks upon the 2nd bookmark, it should run a piece of JavaScript code.
推荐答案
我编写了这段代码,该代码适用于IE,Firefox和Opera(不幸的是,它不适用于Google Chrome).
I wrote this piece of code which works for IE, Firefox and Opera (unfortunately it doesn't work for Google Chrome).
function bookmark()
{
var title = 'Google';
var url = 'http://google.com';
if (document.all) // Check if the browser is Internet Explorer
window.external.AddFavorite(url, title);
else if (window.sidebar) //If the given browser is Mozilla Firefox
window.sidebar.addPanel(title, url, "");
else if (window.opera && window.print) //If the given browser is Opera
{
var bookmark_element = document.createElement('a');
bookmark_element.setAttribute('href', url);
bookmark_element.setAttribute('title', title);
bookmark_element.setAttribute('rel', 'sidebar');
bookmark_element.click();
}
}
这篇关于使用JavaScript添加浏览器书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!