html iframe标签支持手机端展示

1. 获取iframe标签属性 sandbox 

默认

""应用以下所有的限制。
allow-same-origin允许 iframe 内容被视为与包含文档有相同的来源。
allow-top-navigation允许 iframe 内容从包含文档导航(加载)内容。
allow-forms允许表单提交。
allow-scripts允许脚本执行。

JQ设置手机端和电脑端的展示

var isMobile = {
		Android: function () {
			return navigator.userAgent.match(/Android/i) ? true : false;
		},
		BlackBerry: function () {
			return navigator.userAgent.match(/BlackBerry/i) ? true : false;
		},
		iOS: function () {
			return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
		},
		Windows: function () {
			return navigator.userAgent.match(/IEMobile/i) ? true : false;
		},
		any: function () {
			return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
		}
	};
	if (isMobile.any()) {
		//如果是手机访问的话的操作!
		var iframepage = document.getElementById('iframepage');
		$("#iframepage").attr("sandbox", "allow-forms allow-same-origin allow-top-navigation");
	} else {
		var iframepage = document.getElementById('iframepage');
		$("#iframepage").attr("sandbox","allow-scripts allow-forms allow-same-origin allow-top-navigation");
	}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用pdf.js库来在H5手机端iframe中预览PDF文件。下面是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <title>PDF Preview</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js"></script> </head> <body> <iframe id="pdfPreview" style="width:100%;height:100%;border:none;"></iframe> <script> const pdfUrl = "https://example.com/sample.pdf"; const iframe = document.getElementById("pdfPreview"); const pdfjsLib = window['pdfjs-dist/build/pdf']; pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js'; pdfjsLib.getDocument(pdfUrl).promise.then(pdf => { pdf.getPage(1).then(page => { const scale = 1.5; const viewport = page.getViewport({scale: scale}); const canvas = document.createElement("canvas"); const context = canvas.getContext("2d"); canvas.height = viewport.height; canvas.width = viewport.width; const renderContext = { canvasContext: context, viewport: viewport }; page.render(renderContext).promise.then(() => { iframe.src = canvas.toDataURL(); }); }); }); </script> </body> </html> ``` 在上面的示例中,我们使用pdf.js库来获取PDF文件的第一页,并将其渲染到canvas元素中。然后,我们将canvas的数据URL设置为iframe的src属性。这将在iframe中显示PDF文件的预览。请注意,示例中使用的PDF文件URL(pdfUrl)应替换为您自己的PDF文件URL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wcuuchina

谢谢你的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值