社区
JavaScript
帖子详情
JS window.print() 无法连接时报错,且无法try catch
wushang923
2017-07-21 08:40:33
连接是局域网的打印机,当共享打印机的主机开机可以连接时,可以正常调用window.print()打印,但是当共享主机关机或者网络不通时,再调用window.print()就会报错“错误: 远程服务器不存在或不可用”,且无法被try catch捕获。
请问有什么方法可以用JS判断当前调用的打印机是否存在呢?或者用什么方法可以捕获到这个错误呢?
...全文
293
1
打赏
收藏
JS window.print() 无法连接时报错,且无法try catch
连接是局域网的打印机,当共享打印机的主机开机可以连接时,可以正常调用window.print()打印,但是当共享主机关机或者网络不通时,再调用window.print()就会报错“错误: 远程服务器不存在或不可用”,且无法被try catch捕获。 请问有什么方法可以用JS判断当前调用的打印机是否存在呢?或者用什么方法可以捕获到这个错误呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
小伙真帅
2017-07-23
打赏
举报
回复
JS权限不够,应该是调不到打印机状态的。 window.print 调用的是系统默认的打印机,执行打印操作。
图形jquery.gv
Ch
art-1.0.1.min.
js
/* @license * jQuery.
pr
int
, version 1.3.2 * (c) Sathvik Ponangi, Doers' Guild * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) *--------------------------------------------------------------------------*/ (function ($) { "use strict"; // A nice closure for our definitions function getjQueryObject(string) { // Make string a vaild jQuery thing var jqObj = $("");
try
{ jqObj = $(string) .clone(); }
cat
ch
(e) { jqObj = $("
") .html(string); } return jqObj; } function
pr
int
Frame(frame
Window
) { //
Pr
int
the selected
window
/iframe var def = $.Deferred();
try
{ setTimeout(function () { // Fix for IE : Allow it to render the iframe frame
Window
.focus();
try
{ // Fix for IE11 -
pr
int
ng the whole page instead of the iframe content if (!frame
Window
.document.execCommand('
pr
int
', false, null)) { // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 frame
Window
.
pr
int
(); } }
cat
ch
(e) { frame
Window
.
pr
int
(); } frame
Window
.close(); def.resolve(); }, 250); }
cat
ch
(err) { def.reject(err); } return def; } function
pr
int
ContentInNew
Window
(content) { // Open a new
window
and
pr
int
selected content var w =
window
.open(); w.[removed](content); w.document.close(); return
pr
int
Frame(w); } function isNode(o) { /* http://stackoverflow.com/a/384380/937891 */ return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string"); } $.
pr
int
= $.fn.
pr
int
= function () { //
Pr
int
a given set of elements var options, $this, self = this; // console.log("
Pr
int
ing", this, arguments); if (self instanceof $) { // Get the node if it is a jQuery object self = self.get(0); } if (isNode(self)) { // If `this` is a HTML element, i.e. for // $(selector).
pr
int
() $this = $(self); if (arguments.length > 0) { options = arguments[0]; } } else { if (arguments.length > 0) { // $.
pr
int
(selector,options) $this = $(arguments[0]); if (isNode($this[0])) { if (arguments.length > 1) { options = arguments[1]; } } else { // $.
pr
int
(options) options = arguments[0]; $this = $("html"); } } else { // $.
pr
int
() $this = $("html"); } } // Default options var defaults = { globalStyles: true, media
Pr
int
: false, stylesheet: null, no
Pr
int
Selector: ".no-
pr
int
", iframe: true, append: null,
pr
epend: null, manuallyCopyFormValues: true, deferred: $.Deferred() }; // Merge with user-options options = $.extend({}, defaults, (options || {})); var $styles = $(""); if (options.globalStyles) { // Apply the stlyes from the current sheet to the
pr
int
ed page $styles = $("style, link, meta, title"); } else if (options.media
Pr
int
) { // Apply the media-
pr
int
stylesheet $styles = $("link[media=
pr
int
]"); } if (options.stylesheet) { // Add a custom stylesheet if given $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">')); } // Create a copy of the element to
pr
int
var copy = $this.clone(); // Wrap it in a span to get the HTML markup string copy = $("
") .append(copy); // Remove unwanted elements copy.find(options.no
Pr
int
Selector) .remove(); // Add in the styles copy.append($styles.clone()); // Appedned content copy.append(getjQueryObject(options.append)); //
Pr
epended content copy.
pr
epend(getjQueryObject(options.
pr
epend)); if (options.manuallyCopyFormValues) { // Manually copy form values
int
o the HTML for
pr
int
ing user-modified input fields // http://stackoverflow.com/a/26707753 copy.find("input") .ea
ch
(function () { var $field = $(this); if ($field.is("[type='radio']") || $field.is("[type='
ch
eckbox']")) { if ($field.
pr
op("
ch
ecked")) { $field.attr("
ch
ecked", "
ch
ecked"); } } else { $field.attr("value", $field.val()); } }); copy.find("select").ea
ch
(function () { var $field = $(this); $field.find(":selected").attr("selected", "selected"); }); copy.find("textarea").ea
ch
(function () { // Fix for https://github.com/DoersGuild/jQuery.
pr
int
/issues/18#issuecomment-96451589 var $field = $(this); $field.text($field.val()); }); } // Get the HTML markup string var content = copy.html(); // Notify with generated markup & cloned elements - useful for logging, etc
try
{ options.deferred.notify('generated_markup', content, copy); }
cat
ch
(err) { console.warn('Error notifying deferred', err); } // Destroy the copy copy.remove(); if (options.iframe) { // Use an iframe for
pr
int
ing
try
{ var $iframe = $(options.iframe + ""); var iframeCount = $iframe.length; if (iframeCount === 0) { // Create a new iFrame if none is given $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>') .
pr
ependTo('body') .css({ "position": "absolute", "top": -999, "left": -999 }); } var w, wdoc; w = $iframe.get(0); w = w.content
Window
|| w.contentDocument || w; wdoc = w.document || w.contentDocument || w; wdoc.open(); wdoc.write(content); wdoc.close();
pr
int
Frame(w) .done(function () { // Success setTimeout(function () { // Wait for IE if (iframeCount === 0) { // Destroy the iframe if created here $iframe.remove(); } }, 100); }) .fail(function (err) { // Use the pop-up method if iframe fails for some reason console.error("Failed to
pr
int
from iframe", err);
pr
int
ContentInNew
Window
(content); }) .always(function () {
try
{ options.deferred.resolve(); }
cat
ch
(err) { console.warn('Error notifying deferred', err); } }); }
cat
ch
(e) { // Use the pop-up method if iframe fails for some reason console.error("Failed to
pr
int
from iframe", e.stack, e.message);
pr
int
ContentInNew
Window
(content) .always(function () {
try
{ options.deferred.resolve(); }
cat
ch
(err) { console.warn('Error notifying deferred', err); } }); } } else { // Use a new
window
for
pr
int
ing
pr
int
ContentInNew
Window
(content) .always(function () {
try
{ options.deferred.resolve(); }
cat
ch
(err) { console.warn('Error notifying deferred', err); } }); } return this; }; })(jQuery);
可用
js
ajax代码
var xmlHttp; function u_reg(field) { var name=field.value; if(""!=name) { xmlHttp=createXmlHtt
pR
equest(); var url="loginAction!reg?username="+name; xmlHttp.open("GET",url,true); xmlHttp.onreadystate
ch
ange=userName; xmlHttp.send(null); } else { return; } } function
ch
angeDrop(field) { var typeid=field.value; if(""!=name) { xmlHttp=createXmlHtt
pR
equest(); var url="
pr
oductAction!reg?username="+typeid; xmlHttp.open("GET",url,true); xmlHttp.onreadystate
ch
ange=initType; xmlHttp.send(null); } else { return; } } function createXmlHtt
pR
equest() { if(
window
.XMLHtt
pR
equest) { xmlHttp=new XMLHtt
pR
equest(); if(xmlHttp.overideMimeType) { xmlHttp.overidMimeType("text/xml"); } } else if(
window
.ActiveXobject) {
try
{ xmlHttp=new ActiveObject("Msxml2.XMLHTTP"); }
cat
ch
(e) { xmlHttp=new ActiveObject("Microsoft.XMLHTTP"); } } if(!xmlHttp) { alert("您的浏览器不支持创建XMLHTT
PR
equest对象!"); } return xmlHttp; } function userName() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { var u_name=document.getElementById("reg_name"); var uId=xmlHttp.responseText; if(uId=="1") { u_name.innerHTML="用户已存在!"; } else { u_name.innerHTML=""; } } else { alert("请求错误,
无法
验证用户名是否存在!错误代码:"+xmlHttp.status); } } } function initType() { if(xmlHttp.readyState == 4) {//4:表示Ajax引擎初始化成功 if(xmlHttp.status == 200) {//http协议成功 var typeSelect = document.getElementById("typeid"); var deskLength = typeSelect.options.length; for(var j = deskLength;j > 0;j--) { typeSelect.options.remove(j); } var content8 = xmlHttp.responseText; eval(content8); }else { alert("请求失败,错误码=" + xmlHttp.status); } } } public Object reg() {// 用户注册验证由Ajax实现 OutputStream os = null;
Pr
int
Writer pw = null;
try
{ HttpServletResponse res = ServletActionContext.getResponse(); os = res.getOutputStream(); pw = new
Pr
int
Writer(os); if (this.userServices.
ch
eckUsername(this.getUsername())) { pw.
pr
int
(1); } else { pw.
pr
int
(0); } pw.flush(); }
cat
ch
(IOException e) { e.
pr
int
StackTrace(); } finally {
try
{ pw.close(); os.close(); }
cat
ch
(IOException e) { e.
pr
int
StackTrace(); } } return null; }
WEB页面性能测试:页面加载过程+页面性能指标+页面性能工具实战课程
通过页面性能测试概念+页面加载过程+页面性能指标+页面性能测试工具的学习,可以学到如下内容:①WEB网站页面性能的指标(白屏
时
间、首屏加载完成
时
间、FP、FCP、DCL、CLS、FPS等);②WEB网站页面性能测试策略;③WEB网站页面加载过程(前端基础语言:HTML+CSS+JavaScript,DOM、CSSOM、渲染树、布局、绘制);④WEB网站页面性能测试工具的实际操作和功能介绍(开发者工具:控制台、网络、性能、Lighthouse、FPS渲染统计等);⑤WEB网站页面性能测试报告编写。⑥WEB网站页面性能测试步骤和实际操作。课程内容:第一章:课程简介1、课程介绍2、课程大纲第二章:页面性能测试1、页面性能测试概念2、页面性能测试专业术语3、页面性能测试策略第三章:页面加载过程1、HTML概念,HTML5实例,HTML5文档2、CSS概念,CSS实例,CSS文档3、JavaScript概念,JavaScript脚本实例和作用4、DOM和CSSOM理解5、HTML5渲染引擎理解6、HTML5页面内容渲染的过程,HTML渲染树第四章:页面性能测试工具1、页面性能测试具体工具2、
Ch
rome开发者工具介绍第五章:页面性能测试工具-控制台:
window
.performance1、控制台:
window
.performance介绍2、
window
.performance.timing执行结果加载字段理解3、
window
.performance.timing执行结果属性对应页面阶段理解4、
window
.performance页面性能参数计算5、
window
.performance页面性能关键指标计算第六章:页面性能测试工具-网络面板1、
Ch
rome-开发者工具-网络面板介绍2、
Ch
rome-开发者工具-网络-瀑布流指标第七章:页面性能测试工具-性能面板1、
Ch
rome-开发者工具-性能面板-使用方法2、
Ch
rome-开发者工具-性能面板介绍3、
Ch
rome-开发者工具-性能面板-控制按钮区域4、
Ch
rome-开发者工具-性能面板-Overview区域5、
Ch
rome-开发者工具-性能面板-火焰图区域6、
Ch
rome-开发者工具-性能面板-内存图区域7、
Ch
rome-开发者工具-性能面板-统计汇总区域8、
Ch
rome-开发者工具-性能面板-统计汇总区域-摘要内容9、
Ch
rome-开发者工具-性能面板-统计汇总区域-事件
时
长、调用、发生顺序第八章:页面性能测试工具-FPS监控1、
Ch
rome-开发者工具-FPS监控-FPS介绍、FPS视觉效果、FPS查看方法第九章:页面性能测试-Lighthouse面板1、
Ch
rome-开发者工具-Lighthouse介绍、操作方法、运行的生命周期2、
Ch
rome-开发者工具-Lighthouse报告指标分析3、
Ch
rome-开发者工具-Lighthouse的报告优化建议第十章:页面性能测试工具-Performance insights面板1、
Ch
rome-开发者工具-Performance insights操作方法2、
Ch
rome-开发者工具-Performance insights分析报告第十一章:网速调研1、全国网速的调研和本机网速测试第十二章:页面性能测试总结1、页面性能测试指标采集方式2、页面性能测试报告3、页面性能测试-操作步骤
一文弄懂
window
.
pr
int
()打印
一般信息填写类的需求页面,都会增设「预览」和「打印」功能。我们会通过编写 DOM 及样式来绘制出预览视图,而打印则是基于预览来生成 PDF 文件。浏览器原生 API
window
.
pr
int
() 可以用于打印当前窗口(
window
.document)视图内容。调用此方法会产生一个打印预览弹框,用户可以根据具体设置来得到打印结果。
vue,打印案例
window
.
pr
int
() 的完美实现
网页效果图: 调用
window
.
pr
int
()打印的效果: 案例git地址
JavaScript
87,904
社区成员
224,614
社区内容
发帖
与我相关
我的任务
JavaScript
Web 开发 JavaScript
复制链接
扫一扫
分享
社区描述
Web 开发 JavaScript
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章