[英]Unable to display HTML code generated by a PHP script in a new browser window using JQuery
我编写了一个名为generateemail.php的PHP脚本,该脚本接受用户的输入,然后将其输入与模板结合在一起,以构成一个完整的HTML页面,准备发送给客户。
问题是我希望脚本打开显示内容的单独浏览器窗口-我可以使代码传递到新窗口,但是它显示为代码,而不是如屏幕截图所示实际处理。
<script> function htmlEntities(str) { return String(str).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '\\"'); } 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>
像这样尝试一下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 : <meta charset='utf-8' /><title>Sample header</title>
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.