cross-browser print command?

13,945

Solution 1

window.print() is a de-facto standard. (it's been supported since the days of IE4/Netscape 4).

While you're at it, be sure to check out how you can customize how your page looks when it's printed using print-specific CSS stylesheets.

Solution 2

window.print() will do the job.

Share:
13,945
Basit
Author by

Basit

Updated on June 06, 2022

Comments

  • Basit
    Basit almost 2 years

    i want to know if there is any cross-browser print code, that is if i need other then just simple:

    //print page
        $('.print').click(function() {
            window.print();
            return false;
        });
    

    i did found for bookmark and thats why i was more concern about print too, but couldn't find anything useful on google.

    following code is for bookmark cross-browser

    //bookmark page
    $("a.bookmark").click(function(e)
    {
        e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
        var bookmarkUrl = this.href;
        var bookmarkTitle = this.title;
    
        if (window.sidebar) { // For Mozilla Firefox Bookmark
            window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
        } else if( window.external || document.all) { // For IE Favorite
            window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
        } else if(window.opera) { // For Opera Browsers
            $("a.jQueryBookmark").attr("href",bookmarkUrl);
            $("a.jQueryBookmark").attr("title",bookmarkTitle);
            $("a.jQueryBookmark").attr("rel","sidebar");
        } else { // for other browsers which does not support
            alert('Your browser does not support this bookmark action');
            return false;
        }
    });
    
  • balexandre
    balexandre over 10 years
    window.print() invokes nothing on Chrome if you have popup blockers :/
  • broofa
    broofa over 10 years
    @balexandre - link to bug, article, or test case documenting that behavior, please. If that's really the case, it would break a large number of websites that rely on window.print to implement their own print UI. :/
  • balexandre
    balexandre over 10 years
    just try yourself! using AdBlockPlus.
  • broofa
    broofa over 10 years
    @balexandre - window.print() works as expected with both AdBlock and AdBlockPlus extensions in chrome. Perhaps this is just an issue for you? (Hence my request for some supporting documentation of the problem)
  • balexandre
    balexandre over 10 years
    maybe it is just for me, even with the new Chrome v.29 I have the issue :(
  • Henry Blyth
    Henry Blyth almost 10 years
    It's not part of a standard. See stackoverflow.com/a/9269238/3150057. But I see what you mean by "de-facto" as in "it's existed for a long, long time and is used pretty much everywhere."