Javascript 如何在显示弹出窗口之前解码特定元素中的html?
在smarty代码中,我有以下两个javascript函数,我想用它们显示一个包含html标记的动态内容的弹出窗口: 打开弹出窗口的第一个功能:Javascript 如何在显示弹出窗口之前解码特定元素中的html?,javascript,smarty,popupwindow,Javascript,Smarty,Popupwindow,在smarty代码中,我有以下两个javascript函数,我想用它们显示一个包含html标记的动态内容的弹出窗口: 打开弹出窗口的第一个功能: <script type="text/javascript"> // global variable for subwindow reference var newWindow; // generate and fill the new window {literal} function makeNewWindow( ) { // m
<script type="text/javascript">
// global variable for subwindow reference
var newWindow;
// generate and fill the new window
{literal}
function makeNewWindow( ) {
// make sure it isn't already opened
if (!newWindow || newWindow.closed) {
newWindow = window.open("", "prd_window", "height=400, width=300, fullscreen, resizable");
// delay writing until window exists in IE/Windows
setTimeout("writeToWindow( )", 50);
} else if (newWindow.focus) {
// window is already open and focusable, so bring it to the front
newWindow.focus( );
}
}
{/literal}
//子窗口引用的全局变量
新窗口;
//生成并填充新窗口
{literal}
函数makeNewWindow(){
//确保它尚未打开
如果(!newWindow | | newWindow.closed){
新窗口=窗口。打开(“,”prd_窗口“,”高度=400,宽度=300,全屏,可调整大小“);
//延迟写入,直到IE/Windows中存在窗口
setTimeout(“writeToWindow()”,50);
}else if(newWindow.focus){
//窗口已经打开且可聚焦,所以请将其带到前面
focus();
}
}
{/literal}
用于将动态内容写入弹出窗口的第二个函数,从第一个函数调用:
function writeToWindow( ) {ldelim}
// assemble content for new window
var newContent = "<html><head><title>title text here</title></head>";
newContent += "<body>";
var full_descr = "{$markup_info.fulldescr|strip|escape:'html'}";
newContent += "<div id=\"div_fulldescr\">" + full_descr + "</div>";
newContent += "</body></html>";
// write HTML to new window document
newWindow.document.write(newContent);
newWindow.document.close( ); // close layout stream
{rdelim}
</script>
函数writeToWindow(){ldelim}
//为新窗口组装内容
var newContent=“此处的标题文本”;
newContent+=“”;
var full_descr=“{$markup_info.fulldescr | strip | escape:'html'}”;
新内容+=“”+完整描述+“”;
newContent+=“”;
//将HTML写入新窗口文档
newWindow.document.write(newContent);
newWindow.document.close();//关闭布局流
{rdelim}
在第二个函数中,我使用“escape”smarty修饰符对full_descr变量中包含的html标记进行编码,这样javascript代码就不会中断。
$markup\u info是一个智能关联数组,fulldescr是它的索引之一
上面的代码弹出一个窗口,其中div\u fulldescr元素的内容按预期显示为html标记,即未解码。
我想知道在显示弹出窗口之前,如何解码div\u fulldescrdiv元素的html标记 您可能希望将
{$markup\u info.fulldescr | strip | escape:'html'}
替换为{$markup\u info.fulldescr | escape:'javascript'}
太好了,谢谢你的帮助。我不知道“javascript”转义模式——也许我正在阅读smarty文档v.2。因此,正如我所意识到的,没有必要对编码的代码进行解码。它被浏览器本身解码?对。转义:“javascript”只是将字符串转换为javascript可以解析的内容(即转义引号和换行符)