无法使用JQuery在新的浏览器窗口中显示由PHP脚本生成的HTML代码


Unable to display HTML code generated by a PHP script in a new browser window using JQuery

我编写了一个名为generatemail.PHP的PHP脚本,该脚本接受用户的输入,然后将用户的输入与模板组合,组成一个完整的HTML页面,准备发送给客户。

问题是,我希望脚本打开一个单独的浏览器窗口来显示内容——我可以将代码传递到新窗口,但它显示为代码,而不是实际处理它,如屏幕截图所示。

<script>
function htmlEntities(str) {
    return String(str).replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', ''"');
}
function printClick() {
  var w = window.open();
  var html = "<?php echo preg_replace( "/'r|'n/", "", htmlspecialchars($emailheader) ); ?>";
  var html2 = "<?php echo preg_replace( "/'r|'n/", "", htmlspecialchars($emailbody) ); ?>";
  
  
 
  // how do I write the html to the new window with JQuery?
    $(w.document.head).html(html).val();
	$(w.document.body).html(html2).val();
}
</script>

屏幕截图:https://i.stack.imgur.com/cQVyi.png

这样试试http://jsfiddle.net/ty368zfr/

你不能使用jQuery,所以使用纯javascript

 w.document.head.innerHTML = html;
 w.document.body.innerHTML = html2;

我认为问题在于html字符串的连接。试试这样的东西:

var html = '<?php echo preg_replace( "/''r|''n/", "", htmlspecialchars($emailheader) ); ?>';
var html2 = '<?php echo preg_replace( "/''r|''n/", "", htmlspecialchars($emailbody) ); ?>';

您还应该删除val()函数调用。没有必要放置html代码!

$(w.document.head).html(html);
$(w.document.body).html(html2);

问题是使用htmlspecialcharacter对html进行编码。有一个解决方案是删除它

<?php
$emailheader="<meta charset='utf-8' /><title>Sample header</title>";
$emailbody="<p>Some body</p>";
?>
    <script>
        var w = window.open();
      var html = "<?php echo preg_replace( "/'r|'n/", "", htmlspecialchars($emailheader) ); ?>";
      var html2 = "<?php echo preg_replace( "/'r|'n/", "", htmlspecialchars($emailbody) ); ?>";
     console.log(html);//Result :  &lt;meta charset='utf-8' /&gt;&lt;title&gt;Sample header&lt;/title&gt;
      var html = "<?php echo preg_replace( "/'r|'n/", "", $emailheader ); ?>";
      var html2 = "<?php echo preg_replace( "/'r|'n/", "", $emailbody ); ?>";
      console.log(html);//Result : <meta charset='utf-8' /><title>Sample header</title>
        w.document.head.innerHTML = html;
        w.document.body.innerHTML = html2;
    </script>

我使用Base64 解决了这个问题

<script>

var Base64 = {
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    decode: function(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9'+'/'=]/g, "");
        while (i < input.length) {
            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = Base64._utf8_decode(output);
        return output;
    },
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while (i < utftext.length) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

function printClick() {
  var w = window.open();
  var html = "<?php echo $emailheader; ?>";
  var html2 = "<?php echo $emailbody; ?>";

  var decodedString = Base64.decode(html);
  var decodedString2 = Base64.decode(html2);

$(w.document.head).html(decodedString);
$(w.document.body).html(decodedString2);
}
</script>