只使用url打印网页而不打开新窗口? [英] Printing a web page using just url and without opening new window?
问题描述
< HTML>
< head>
< script type =text / javascript>
var URL =http:// localhost:8000 / foobar /;
var W = window.open(URL); ** Note1 **
W.window.print();
< / script>
< / head>
< p>打印ME ...............< / p>
< / html>
我正在使用此脚本来打印网页。
我的视图渲染了这个页面,JS注意到其他所有的东西。
但我不想为此打开新窗口。所以,我应该用什么来代替 window.open(URL)
so 没有新窗口
会打开。同样,我不想为打印功能
打开新窗口。因此,每次渲染此页面时,它都会在同一页面上执行所有操作。没有新窗口,没有新标签。我怎样才能做到这一点。我谷歌,但似乎没有工作。您可以使用隐藏的 iFrame 来执行此操作(我使用jquery作为示例) :
function loadOtherPage(){
$(< iframe>)// create一个新的iframe元素
.hide()//使其不可见
.attr(src,/ url / to / page / to / print)//将iframe指向您的页面要打印
.appendTo(body); //将iframe添加到DOM以使其加载页面
}
这将加载您想要打印的页面。要打印,您可以将javascript代码添加到打印页面,以便在加载后打印:
$(document).ready (function(){
window.print();
});
这将打印页面而不显示新窗口。我已经在IE8,9和谷歌浏览器中测试过了,所以我不确定这是否适用于Safari或Firefox。
<html>
<head>
<script type="text/javascript">
var URL = "http://localhost:8000/foobar/";
var W = window.open(URL); **Note1**
W.window.print();
</script>
</head>
<p> Print ME...............</p>
</html>
I am using this script to print a webpage. My views render this page and The JS take care all other things.
But I dont want to open new window for that. So, What should I use instead of window.open(URL)
so no new window
opens. Similarly, I don't want to open new window for print function
.So, Whenever I render this page it do all stuff on the same page. No new window, No new tab. How can I achieve this. I google but nothing seems working.
You can do this using a hidden iFrame (I'm using jquery for the example):
function loadOtherPage() {
$("<iframe>") // create a new iframe element
.hide() // make it invisible
.attr("src", "/url/to/page/to/print") // point the iframe to the page you want to print
.appendTo("body"); // add iframe to the DOM to cause it to load the page
}
This will load the page you want to print. To print, you can add javascript code to the print page so that it gets printed after loading:
$(document).ready(function () {
window.print();
});
This will print the page without showing a new window. I've tested this in IE8,9 and Google Chrome, so I'm not sure if this works for Safari or Firefox, though.
这篇关于只使用url打印网页而不打开新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!