您现在的位置:首页 >> 前端 >> 内容

打印html并去掉页眉页脚

时间:2017/6/21 9:33:53 点击:

  核心提示:win10下测试ie11,chrome,firefox,edge 都可以成功去掉页眉页脚!DOCTYPE htmlhtmlheadtitleprint/titlemeta charset=utf-8s...

win10下测试ie11,chrome,firefox,edge 都可以成功去掉页眉页脚

<!DOCTYPE html>
<html>

<head>
    <title>print</title>
    <meta charset="utf-8">
    <style media="print">
    @page {
        size: auto;
        margin: 0mm;
    }
    </style>
</head>

<body>
    <p style="width:300px; height:300px;border: 1px solid blue;">
        <label>label</label>
        <button>button</button>
    </p>
    <button onclick='print_page()'>print</button>
</body>

<script type="text/javascript">
function print_page() {
    if (!!window.ActiveXObject || "ActiveXObject" in window) {
        remove_ie_header_and_footer();
    }
    window.print();
};

function remove_ie_header_and_footer() {
    var hkey_root, hkey_path, hkey_key;
    hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
    try {
        var RegWsh = new ActiveXObject("WScript.Shell");
        RegWsh.RegWrite(hkey_path + "header", "");
        RegWsh.RegWrite(hkey_path + "footer", "");
    } catch (e) {}
}
</script>

</html>

Tags:打印 印H HT TM 
作者:网络 来源:QF