Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 781099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:10:49+00:00 2026-05-14T20:10:49+00:00

I’m writing the JS for a chat application I’m working on in my free

  • 0

I’m writing the JS for a chat application I’m working on in my free time, and I need to have HTML identifiers that change according to user submitted data. This is usually something conceptually shaky enough that I would not even attempt it, but I don’t see myself having much of a choice this time. What I need to do then is to escape the HTML id to make sure it won’t allow for XSS or breaking HTML.

Here’s the code:

var user_id = escape(id)
var txt = '<div class="chut">'+
            '<div class="log" id="chut_'+user_id+'"></div>'+
            '<textarea id="chut_'+user_id+'_msg"></textarea>'+
            '<label for="chut_'+user_id+'_to">To:</label>'+
            '<input type="text" id="chut_'+user_id+'_to" value='+user_id+' readonly="readonly" />'+
            '<input type="submit" id="chut_'+user_id+'_send" value="Message"/>'+
          '</div>';

What would be the best way to escape id to avoid any kind of problem mentioned above? As you can see, right now I’m using the built-in escape() function, but I’m not sure of how good this is supposed to be compared to other alternatives. I’m mostly used to sanitizing input before it goes in a text node, not an id itself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:10:50+00:00Added an answer on May 14, 2026 at 8:10 pm

    Never use escape(). It’s nothing to do with HTML-encoding. It’s more like URL-encoding, but it’s not even properly that. It’s a bizarre non-standard encoding available only in JavaScript.

    If you want an HTML encoder, you’ll have to write it yourself as JavaScript doesn’t give you one. For example:

    function encodeHTML(s) {
        return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
    }
    

    However whilst this is enough to put your user_id in places like the input value, it’s not enough for id because IDs can only use a limited selection of characters. (And % isn’t among them, so escape() or even encodeURIComponent() is no good.)

    You could invent your own encoding scheme to put any characters in an ID, for example:

    function encodeID(s) {
        if (s==='') return '_';
        return s.replace(/[^a-zA-Z0-9.-]/g, function(match) {
            return '_'+match[0].charCodeAt(0).toString(16)+'_';
        });
    }
    

    But you’ve still got a problem if the same user_id occurs twice. And to be honest, the whole thing with throwing around HTML strings is usually a bad idea. Use DOM methods instead, and retain JavaScript references to each element, so you don’t have to keep calling getElementById, or worrying about how arbitrary strings are inserted into IDs.

    eg.:

    function addChut(user_id) {
        var log= document.createElement('div');
        log.className= 'log';
        var textarea= document.createElement('textarea');
        var input= document.createElement('input');
        input.value= user_id;
        input.readonly= True;
        var button= document.createElement('input');
        button.type= 'button';
        button.value= 'Message';
    
        var chut= document.createElement('div');
        chut.className= 'chut';
        chut.appendChild(log);
        chut.appendChild(textarea);
        chut.appendChild(input);
        chut.appendChild(button);
        document.getElementById('chuts').appendChild(chut);
    
        button.onclick= function() {
            alert('Send '+textarea.value+' to '+user_id);
        };
    
        return chut;
    }
    

    You could also use a convenience function or JS framework to cut down on the lengthiness of the create-set-appends calls there.

    ETA:

    I’m using jQuery at the moment as a framework

    OK, then consider the jQuery 1.4 creation shortcuts, eg.:

    var log= $('<div>', {className: 'log'});
    var input= $('<input>', {readOnly: true, val: user_id});
    ...
    

    The problem I have right now is that I use JSONP to add elements and events to a page, and so I can not know whether the elements already exist or not before showing a message.

    You can keep a lookup of user_id to element nodes (or wrapper objects) in JavaScript, to save putting that information in the DOM itself, where the characters that can go in an id are restricted.

    var chut_lookup= {};
    ...
    
    function getChut(user_id) {
        var key= '_map_'+user_id;
        if (key in chut_lookup)
            return chut_lookup[key];
        return chut_lookup[key]= addChut(user_id);
    }
    

    (The _map_ prefix is because JavaScript objects don’t quite work as a mapping of arbitrary strings. The empty string and, in IE, some Object member names, confuse it.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.