admin管理员组

文章数量:1279042

I've written a code that has successfully created a bookmark for any of the following browsers - IE, Firefox and Opera.

<script language="JavaScript" type="text/javascript">
    function bookmark() 
    {
        var title = 'Google';
        var url = '';

        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();
        }
    }
</script>

Now I want my bookmark to run a piece of JavaScript code instead of surfing to Google, when the user clicks on it.

I've written a code that has successfully created a bookmark for any of the following browsers - IE, Firefox and Opera.

<script language="JavaScript" type="text/javascript">
    function bookmark() 
    {
        var title = 'Google';
        var url = 'http://google.';

        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();
        }
    }
</script>

Now I want my bookmark to run a piece of JavaScript code instead of surfing to Google, when the user clicks on it.

Share Improve this question edited Jun 3, 2010 at 13:15 David M 72.9k13 gold badges163 silver badges187 bronze badges asked Jun 3, 2010 at 13:12 Arjun VasudevanArjun Vasudevan 8724 gold badges15 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

This is called a bookmarklet. You could try replacing 'http://google.' with "javascript:alert('Annoying message');". However, Firefox at least doesn't allow adding bookmarklets using this API. I suspect IE and Opera may be the same.

You can try putting the js code in an html and then bookmark that html.

本文标签: clickRunning a JavaScript code when a browser bookmark is clickedStack Overflow