简体   繁体   English

如何使浏览器不解析从JavaScript添加的新HTML代码

[英]How to make browser NOT parse new HTML code that added from javascript

I want to add some text to my webpage from my server with ajax, but the text includes some html tags, and browsers tend to parse that code and render it. 我想使用ajax从服务器向网页中添加一些文本,但是该文本包含一些html标签,浏览器倾向于解析该代码并将其呈现。 I don't want this behavior, i want to appent that text without parsing the html tags. 我不想这种行为,我想在不解析html标签的情况下显示该文本。 Is there way to accomplish this? 有办法做到这一点吗?

you have to encode the returned "HTML". 您必须返回的“ HTML”进行编码 basically you have to replave < , > and & with &lt; 基本上,您必须用&lt;替换<>& &lt; , &gt; &gt; and &amp; &amp; .

if you're using PHP on serverside, for example, you could do this using htmlspecialchars() - there might be similar functions or methods available for other languages ( cgi.escape for python, HTML:Entities for Perl, System.Web.HttpUtility.HtmlEncode for asp.net ...). 例如,如果您在服务器端使用PHP,则可以使用htmlspecialchars() -其他语言可能有类似的功能或方法(python的cgi.escape ,Perl的HTML:EntitiesSystem.Web.HttpUtility .HtmlEncode for asp.net ...)。

if you do this on a retuned string that looks like 如果您对看起来像这样的经过重新调整的字符串执行此操作

this is <strong>bold</strong> text

wich is shown in your browser as 夹在您的浏览器中显示为

this is bold text 这是粗体

your output will change to 您的输出将更改为

this is &lt;strong&gt;bold&lt;/strong&gt; text

wich will display as wich将显示为

this is <strong>bold</strong> text 这是<strong>粗体</ strong>文字

Use node.textContent property (element.innerText on IE<=8) to assign textual content to an element. 使用node.textContent属性(IE <= 8上的element.innerText)将文本内容分配给元素。 Browser will encode all special characters into the respective HTML entities: 浏览器会将所有特殊字符编码为各自的HTML实体:

var h1 = document.getElementsByTagName("h1")[0];
if (typeof h1.textContent != "undefined") {
    h1.textContent = "<b>Hello World</b>";
}
else if (typeof h1.innerText != "undefined") {
    h1.innerText = "<b>Hello World</b>";
}
alert(h1.innerHTML);​
// at this point, the h1 tag will "render" <b>Hello World</b>
// not the text Hello World in bold face
// inspecting the element will reveal that html tags, e.g. <b>
// were converted to &lt;b&gt;

demo 演示

In browser-side javascript: 在浏览器端的javascript中:

function htmlEncode( str )  {
    str = str + "";
    var el = document.createElement("div");
    el.appendChild( document.createTextNode( str ) );
    return el.innerHTML;
}

htmlEncode( "<div></div>" );
//"&lt;div&gt;&lt;/div&gt;"

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

相关问题 Javascript如何使“代码”出现在浏览器中并带有用户的更新 - Javascript How to make 'code' appear in the browser with updates from user 如何使用 javascript 从浏览器复制 HTML 代码 - How to copy HTML code from the browser using javascript 如何使 JavaScript 中添加的元素与 HTML 中添加的元素反应相同? - How to make elements added in JavaScript react the same as added in HTML? 如何在python中的html源中解析javascript代码? - How to parse javascript code in html source in Python? 如何解析包含 javascript 代码的 html - How to parse html that includes javascript code 如何通过javascript代码从客户端浏览器的浏览器发出一个请求而不是一个请求? - how to make one request instead of dozen from browser in client browser through javascript code? 使倒数计时功能从html解析新值 - Make countdown function to parse the new value from html 如何将工作 HTML、CSS、Javascript 代码从 Codepen 传输到 Visual Studio 代码和浏览器 - How to Transfer Working HTML, CSS, Javascript Code from Codepen to Visual Studio Code and Browser 如何在Firefox中从JavaScript解析HTML?(2) - How to parse HTML from JavaScript in Firefox?(2) 如何在Firefox中解析JavaScript中的HTML? - How to parse HTML from JavaScript in Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM