简体   繁体   English

使用 PHP 调用 Javascript 函数

[英]Call Javascript function using PHP

is it possible to call this function to print a document using PHP?是否可以调用此函数来使用 PHP 打印文档?

Here is my code guys..这是我的代码伙计们..

<html>
<head>
<script language="javascript">
   function Clickheretoprint()
   { 
     var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
         disp_setting+="scrollbars=yes,width=900, height=700, left=100, top=25";  

     var docprint=window.open("","",disp_setting); 
         docprint.document.open(); 
         docprint.document.write('<html><head><title>Testing</title>'); 
         docprint.document.write('</head><body onLoad="self.print()" style="width: 900px; height="auto" font-size:16px; font-family:arial;">');          
         docprint.document.write('<h1>Hello World!</h1>');          
         docprint.document.write('</body></html>'); 
         docprint.document.close(); 
         docprint.focus(); 
     }
</script>
</head>
<body>
</body>
</html>

Thank you.谢谢你。

Your javascript function works as written (opens a new window, writes to it, then opens the print dialog), and I've inserted the function call using PHP below:您的 javascript 函数按编写的方式工作(打开一个新窗口,写入它,然后打开打印对话框),并且我已经使用下面的 PHP 插入了函数调用:

<script language="javascript">
function Clickheretoprint() {
    var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
    disp_setting+="scrollbars=yes,width=900, height=700, left=100, top=25";

    var docprint=window.open("","",disp_setting);
    docprint.document.open();
    docprint.document.write('<html><head><title>Testing</title>');
    docprint.document.write('</head><body onLoad="self.print()" style="width: 900px; height="auto" font-size:16px; font-family:arial;">');
    docprint.document.write('<h1>Hello World!</h1>');
    docprint.document.write('</body></html>');
    docprint.document.close();
    docprint.focus();
}
<?php
//calling function using PHP
echo 'Clickheretoprint();';
?>
</script>

I think you're having another issue:我想你有另一个问题:

  • Is your browser preventing popups?您的浏览器是否阻止弹出窗口? (If that's the case, the window is never open, therefore docprint.document is undefined) (如果是这种情况,窗口永远不会打开,因此docprint.document未定义)
  • You probably don't need to call the function via PHP - but perhaps using a click event in javascript.您可能不需要通过 PHP 调用该函数 - 但可能需要在 javascript 中使用 click 事件。

In addition, docprint.document.open();另外, docprint.document.open(); is unnecessary, considering you already opened the window with var docprint=window.OPEN("","",disp_setting);没有必要,考虑到您已经使用var docprint=window.OPEN("","",disp_setting);打开了窗口var docprint=window.OPEN("","",disp_setting);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM