我有一个.net应用程序动态创建一个小的HTML页面,并使用JavaScript document.open方法弹出它在一个新窗口。与功能一切正常。
现在我想添加一个按钮,打印页面的HTML页面。我已经尝试使用下面的code无济于事的尝试:
< A HREF =print.html'的onClick ='window.print();返回false;'>
< IMG SRC =图像/ printer.png高度='32PX'宽='32PX'>< / A>
当被点击,在弹出的窗口中的按钮,没有任何反应。但是,当这个页面的源$ C $ C的保存和加载在浏览器作为一个单独的页面,打印按钮完美的作品。 所以,这样看来,这个问题是由一个事实,即code是在一个弹出窗口。 [现在的问题似乎是,在写入弹出的窗口中code它打开后。]有谁知道一个办法解决这个问题,或者其它方法吗?
编辑:
其他的方法,我曾尝试与相同的结果:
<输入类型=按钮的onclick ='window.print()'值=打印/>
和
< A HREF =JavaScript的:window.print()'>
< IMG SRC =图像/ printer.png高度='32PX'宽='32PX'>< / A>
再次编辑:
以上code在Firefox,但不是在IE7。对周围的工作IE浏览器的任何想法?
编辑再次:
下面是使用code一个测试用例, npup posted下面。取而代之的是code的弹出窗口生活在一个单独的HTML文件,我打开一个空白的URL,然后写code到它。这一步似乎是什么原因造成的问题。
< HTML>
< HEAD>
<冠军>主< /标题>
< /头>
<身体GT;
< H1>
POP和放大器;打印< / H1>
<按钮的onclick =弹出();>
流行LT; /按钮>
<脚本类型=文/ JavaScript的>
VAR POP;
功能弹出(){
变种newWin =的window.open('','thePopup','宽度= 350,高度= 350');
newWin.document.write("<html><head><title>popup</title></head><body><h1>Pop</h1>" +
&LT; P&GT;打印ME&LT; / P&GT;&LT; A HREF =print.html'的onclick ='window.print();返回false;&gt;中+
&LT; IMG SRC =图像/ printer.png高度='32PX'宽='32PX'&GT;&LT; / A&GT;&LT; / BODY&GT;&LT; / HTML&gt;中);
}
&LT; / SCRIPT&GT;
&LT; /身体GT;
&LT; / HTML&GT;
解决方案
我已经用标准的HTML标记创建一个空白的HTML页面解决了这个问题。然后,我通过创建一个新的DOM元素和编辑的innerHTML添加的内容。所得code是相同的例子中,简单地用下面的取代newWin.document.write命令:
VAR newDiv = newWin.document.createElement('格');
newDiv.innerHTML =&LT; H1&GT;流行LT; / H1&gt;中+
&LT; P&GT;打印ME&LT; / P&GT;&LT; A HREF =print.html'的onclick ='window.print();返回false;&gt;中+
&LT; IMG SRC =图像/ printer.png高度='32PX'宽='32PX'&GT;&LT; / A&gt;中
newWin.document.body.appendChild(newDiv);
虽然这个问题已经解决了,我真的不知道是什么问题的确切原因。如果任何人有任何想法,我将很高兴听到他们的声音。
I have a .Net application that dynamically creates a small HTML page and pops it up in a new window using the javascript document.open method. Everything with that functionality is working fine.
Now I want to add a button to the HTML page that prints the page. I have tried using the following code to no avail:
<a href='print.html' onClick='window.print();return false;'>
<img src='images/printer.png' height='32px' width='32px'></a>
When the button is clicked in the popup window, nothing happens. But when the source code of of this page is saved and loaded in a browser as a separate page, the print button works perfectly. So it would appear that the problem is caused by the fact that the code is in a popup window. [The problem now appears to be that the code in written to the popup window after it is opened.] Does anyone know a way to fix this problem or any alternatives?
EDIT:
Other method that I have tried with the same results:
<input type='button' onclick='window.print()' value='Print' />
and
<a href='javascript:window.print()'>
<img src='images/printer.png' height='32px' width='32px'></a>
EDIT AGAIN:
The above code works in Firefox, but not in IE7. Any ideas on a work around for IE?
EDIT YET AGAIN:
Here is a test case using the code that npup posted below. Instead of the code for the popup window living in a separate html file, I am opening a blank url and then writing the code to it. This step appears to be what is causing the problem.
<html>
<head>
<title>main</title>
</head>
<body>
<h1>
Pop & print</h1>
<button onclick="pop();">
Pop</button>
<script type="text/javascript">
var POP;
function pop() {
var newWin = window.open('', 'thePopup', 'width=350,height=350');
newWin.document.write("<html><head><title>popup</title></head><body><h1>Pop</h1>" +
"<p>Print me</p><a href='print.html' onclick='window.print();return false;'>" +
"<img src='images/printer.png' height='32px' width='32px'></a></body></html>");
}
</script>
</body>
</html>
解决方案
I have solved the problem by creating a blank HTML page with the standard HTML markup. I then added the content by creating a new DOM element and editing the innerHTML. The resulting code is the same as in the example, simply replacing the newWin.document.write command with the following:
var newDiv = newWin.document.createElement( 'div' );
newDiv.innerHTML = "<h1>Pop</h1>" +
"<p>Print me</p><a href='print.html' onclick='window.print();return false;'>" +
"<img src='images/printer.png' height='32px' width='32px'></a>"
newWin.document.body.appendChild(newDiv);
While the issue has been resolved, I am honestly not sure what was the exact cause of the problem. If anyone has any ideas, I would be glad to hear them.