Javascript 打印 div 在 Safari 网络浏览器中不起作用

标签 javascript jquery safari

这是我使用的代码,在firefox和chrome中都可以正常使用,不幸的是safari不支持这个对话框出现后立即关闭。这个问题有什么解决办法吗?

 var mywindow = window.open('', 'PRINT', 'height=400,width=600');
 mywindow.document.write(
   '<html style="background-color: white;"><head><title></title>');
 mywindow.document.write(
   '<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/queue.css">'
 );
 mywindow.document.write(
   '</head><body style="background-color: white !important;padding-top:15px;margin:2">'
 );
 mywindow.document.write(document.getElementById('forprint').innerHTML);
 mywindow.document.write('</body></html>');
 mywindow.document.close(); // necessary for IE >= 10
 mywindow.focus(); // necessary for IE >= 10*/
 mywindow.print();
 mywindow.close();
 return true;

最佳答案

Safari 阻止对 window.open() 的调用(异步调用)在进行 asnyc 调用之前先调用 window.open 然后设置位置(当 promise 已解决时):

var newWindow = window.open();
var url = "[YOURURL]";

service.getUrl().then(function(url) {
     newWindow.location = url;
});

关于Javascript 打印 div 在 Safari 网络浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50055604/

相关文章:

java - 如何在 Safari 中激活 Java 和 Java 控制台?

javascript - 在 Angular JS 中观察全局对象的属性

php - 如何使用 javascript 重新提交 PHP 表单

javascript - 我什么时候应该使用 KnockoutJS 组件和模板?

javascript - jQuery UI ReplaceWith() 动画?

php - 在我的网站中通过鼠标右键单击来控制链接

safari - try catch 音频但 navigator.mediaDevices.enumerateDevices() 在 Safari 12 上为 NULL,即使已授予麦克风权限

javascript - 遍历 javascript 对象以获取键值对

jquery - 获取 Fullcalendar v4 外部事件列表中的对象 ID

ios - AngularStrap 日期选择器不会在 IOS Safari 浏览器中触发事件模糊时消失