What do I need to escape inside the html
 tag

前端 未结 4 1215
栀梦
栀梦 2021-02-07 03:19

I use the

 tag in my blog to post code. I know I have to change < to < and > to >         


        
4条回答
  •  天涯浪人
    2021-02-07 04:04

    The "Only For You" - HTML "fosile" version: using </code> tag</h1> <p>This is not well known, but it really does exist and even chrome still supports it, however using <strong>pair</strong> <code><xmp></code> tag is <strong>NOT recommended</strong> to be relied on - it's just for you, but its a very simple way how to do your personal e.g. DOCS. even w3.org WIKI says in example "No, really. don't use it."</p> <p>You can put ANY html (excluding <code> end tag) inside

    
    <html> <br> just any other html tags...
    
    

    The proper version

    Proper version could be considered a HTML stored as STRING and displayed with the help of some escaping function.
    Just remember one thing - the strings in C-like languages are ususally written between single quotes or double quotes - if you wrap your string in double => you should escape doubles (problably with \), if you wrap your string in single => escape singles (probably with \)...

    The most common way - Server-side language escaping (ex. in PHP)

    Server-side scripting languages often have some built-in function to escape HTML.

     
    or just any other HTML"; //store html echo htmlspecialchars($html); //display escaped html ?>

    The client-side way (example in JavaScript&jQuery)

    Similar approach as on server-side is achievable in client-side scripts, JavaScript, from what I know, has no built-in function for that (it's quite logical), but if you use some framework/library, like jQuery - there are functions that can be used that way.
    Just remember the same thing as for server-side - in C-like languages, escape the quotes you've wrapped your string in...

    var html = ' 
    or just any other HTML'; var $elementToInsertEscapedHTMLto = jQuery("XXX"); //XXX is selector, e.g. CSS selector $elementToInsertEscapedHTMLto.text( html );

提交回复
热议问题