如何在打印前等待javascript文件加载twitter,并在打印后关闭选项卡
How to wait for javascript file to load twitter before printing and close the tab after printing
嘿,我正在使用推特嵌入的延迟加载来加载到我的页面上。目前我已经给出了2000的setTimeout
,之后我使用window.print()
打印页面。但是,如果网络连接很慢,那就不起作用,如果网络速度好,那就是浪费时间。此外,打印后,我需要关闭我使用window.close()
的选项卡
block main-script
// build:js /js/lazy-x.js
script(src="/js/lazy-x.js")
// endbuild
script.
document.addEventListener("DOMContentLoaded", myFunction);
function myFunction() {
setTimeout(function(){
var file_name = ("{{article['title']}}".replace(/[^0-9a-zA-Z_?: .]/g, ""));
document.title="Scroll - "+file_name;
window.print();}, 2000);
window.onfocus=function(){ window.close();}
}
这是我正在处理的jade
文件。我需要等待lazy-x.js
加载每个twitter嵌入,然后会出现打印对话框,打印后我需要关闭选项卡。但选项卡没有关闭。
我不确定你的问题是否正确。您可以在脚本上执行onLoad:
function importScript (sSrc, fOnload) {
var oScript = document.createElement("script");
oScript.type = "text'/javascript";
oScript.onerror = loadError;
if (fOnload) { oScript.onload = fOnload; }
document.currentScript.parentNode.insertBefore(oScript, document.currentScript);
oScript.src = sSrc;
}
示例来自:https://developer.mozilla.org/en/docs/Web/API/HTMLScriptElement
那么你的代码是:
importScript('/js/lazy-x.js', function() {
var file_name = ("{{article['title']}}".replace(/[^0-9a-zA-Z_?: .]/g, ""));
document.title="Scroll - "+file_name;
window.print()
});
也可以在玉石模板中进行
script(type="text/javascript" src="path_to_js" onload ="callMyFunction()")
另一件事,在window.focus上,你想关闭窗口,它可能会阻止打印/停止JS。代替window.focus检查window.onafterprint
或window.onbeforeprint
事件
相关文章:
- 设置选项对象关键点:值对以实现带第二个y轴的动态打印
- 如何在打印前等待javascript文件加载twitter,并在打印后关闭选项卡
- 如何提供保存文件而不是从internet explorer打印文件的选项
- 如何在不包含导航选项卡内容、页脚和头部的情况下打印 HTML/PHP 页面
- 正在从开发人员工具中的select元素打印选项列表
- 从网页打印后用javascript关闭选项卡
- 设置饼图的打印选项的格式
- 将子元素打印为 AngularJS 选择的 ng-选项
- 在 C# 中查看 PDF 文件并阻止保存和打印选项
- 使用 jQuery 在文本框内打印所选选项的计数和值
- 使用多个选项卡打印网站
- 是否有任何选项可以打印 c3js 生成的图形
- 打印HTML而不在新选项卡中打开
- HTML/Javascript启动打印选项
- jquery UI 选项卡内容在单击按钮时打印
- 在新选项卡中打印内容将打印空白页
- 如何为所有浏览器创建自己的打印预览选项
- 嵌套选项不打印所有路径
- 打开打印选项卡时总是显示“正在加载”;图标
- 为打印选项javascript创建个人快捷方式