cross-browser print command?

2019-02-13 10:40发布

问题:

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;
    }
});

回答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.



回答2:

window.print() will do the job.



回答3:

That is the general way. It is not an official part of the dom. I would check for its existence first.