22 三月, 2009
Use JavaScript to Print IFrame Dynamic Content
Posted by: asd In: Code Snippet| Script| Web| 程式設計
view plain
CODE:
<script type="text/javascript">
window.onload=function()
{
var iframe =document.createElement('iframe');
iframe.width=0;
iframe.height=0;
document.body.appendChild(iframe);
if(iframe)
{
//var iframe = document.getElementById("myframe");
var ifWin = iframe.contentWindow || iframe;
ifWin.document.open();
ifWin.document.write("<html>1111<a href='javascript:alert(\"hello\")'>Hello</a></body>");
ifWin.document.write("</html>");
ifWin.document.close();
ifWin.focus();
ifWin.print();
}
iframe.parentNode.removeChild(iframe);
return false;
}
</script>
若指定 iframe 屬性 src = http://xxx.domain.xxx/page.htm ,該 domain 必需是 local domain 才能列印,否則會顯示存取被拒的錯誤訊息。
參考 : Working with IFRAME in JavaScript