使用 JavaScript 添加浏览器书签

发布于 2024-09-04 05:27:08 字数 272 浏览 4 评论 0原文

我有一个 ASP.NET 网页,其中有一个按钮。单击该按钮,书签应保存在浏览器中,当用户单击书签时,应浏览到 http://google.com

我如何确保它适用于几乎所有标准浏览器或至少适用于 IE、Mozilla Firefox、Opera 和 Google Chrome。

另一种情况,我也以同样的方式创建第二个书签。但是当用户单击第二个书签时,它应该运行一段 JavaScript 代码。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑脸一如从前 2024-09-11 05:27:08

我编写的这段代码适用于 IE、Firefox 和 Opera(不幸的是它不适用于 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();
    }
}

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();
    }
}
黎歌 2024-09-11 05:27:08

出于安全原因无法完成。过去可以使用专有的 IE 命令,但我认为这在 IE 7 中就结束了。在其他版本中绝对不可能。

Mozilla Developer Central 上的相关讨论

Can't be done for security reasons. It used to be possible using a proprietary IE command but I think that ended in IE 7. Definitely impossible in the others.

Related discussion on Mozilla Developer Central

暖伴 2024-09-11 05:27:08

据我所知,Firefox 目前没有添加常规书签的功能。 Firefox 中只能创建侧边栏书签。

您可以在这里找到适用于大多数浏览器的脚本: http://labnol.blogspot.com/2006/01/add-to-favorites-ie-bookmark-firefox.html

正如您在这一行中看到的:

window.sidebar.addPanel(title, url,"");

它只为 Firefox 添加了一个侧边栏条目,这对用户来说不太友好。

In Firefox as far as I know there is currently no function that adds a regular bookmark. Only a sidebar bookmark can be created in Firefox.

Here you can find a script that works for most browsers: http://labnol.blogspot.com/2006/01/add-to-favorites-ie-bookmark-firefox.html

As you can see at this line:

window.sidebar.addPanel(title, url,"");

it only adds a sidebar entry for Firefox, which is not very user friendly.

拧巴小姐 2024-09-11 05:27:08

对于FireFox,不需要为书签设置任何javascript,只有带有 titlerel="sidebar" 的锚标记可以执行此功能

<a href="http://www.google.com" title="Google" rel="sidebar">Bookmark This Page</a>

我已经在 FF9 上测试过

For FireFox no need to set any javascript for the bookmark, only an anchor tag with title and rel="sidebar" can do this functionality

<a href="http://www.google.com" title="Google" rel="sidebar">Bookmark This Page</a>

I have tested it on FF9

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文