Skip to content

Files

Latest commit

 

History

History
52 lines (46 loc) · 1.14 KB

index.md

File metadata and controls

52 lines (46 loc) · 1.14 KB

web打印代码

Browser.js

const Browser = {
  // Firefox 1.0+
  isFirefox: () => {
    return typeof InstallTrigger !== 'undefined'
  },
  // Internet Explorer 6-11
  isIE: () => {
    return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode
  },
  // Edge 20+
  isEdge: () => {
    return !Browser.isIE() && !!window.StyleMedia
  },
  // Chrome 1+
  isChrome: (context = window) => {
    return !!context.chrome
  },
  // At least Safari 3+: "[object HTMLElementConstructor]"
  isSafari: () => {
    return Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 ||
        navigator.userAgent.toLowerCase().indexOf('safari') !== -1
  },
  // IOS Chrome
  isIOSChrome: () => {
    return navigator.userAgent.toLowerCase().indexOf('crios') !== -1
  }
}

export default Browser

code

// If Edge or IE, try catch with execCommand
if (Browser.isEdge() || Browser.isIE()) {
  try {
    iframeElement.contentWindow.document.execCommand('print', false, null)
  } catch (e) {
    iframeElement.contentWindow.print()
  }
} else {
  // Other browsers
  iframeElement.contentWindow.print()
}