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 7024663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:52:17+00:00 2026-05-27T23:52:17+00:00

On the one hand if I have <script> var s = ‘Hello </script>’; console.log(s);

  • 0

On the one hand if I have

<script>
var s = 'Hello </script>';
console.log(s);
</script>

the browser will terminate the <script> block early and basically I get the page screwed up.

On the other hand, the value of the string may come from a user (say, via a previously submitted form, and now the string ends up being inserted into a <script> block as a literal), so you can expect anything in that string, including maliciously formed tags. Now, if I escape the string literal with htmlentities() when generating the page, the value of s will contain the escaped entities literally, i.e. s will output

Hello &lt;/script&gt;

which is not desired behavior in this case.

One way of properly escaping JS strings within a <script> block is escaping the slash if it follows the left angle bracket, or just always escaping the slash, i.e.

var s = 'Hello <\/script>';

This seems to be working fine.

Then comes the question of JS code within HTML event handlers, which can be easily broken too, e.g.

<div onClick="alert('Hello ">')"></div>

looks valid at first but breaks in most (or all?) browsers. This, obviously requires the full HTML entity encoding.

My question is: what is the best/standard practice for properly covering all the situations above – i.e. JS within a script block, JS within event handlers – if your JS code can partly be generated on the server side and can potentially contain malicious data?

  • 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-27T23:52:17+00:00Added an answer on May 27, 2026 at 11:52 pm

    The following characters could interfere with an HTML or Javascript parser and should be escaped in string literals: <, >, ", ', \, and &.

    In a script block using the escape character, as you found out, works. The concatenation method (</scr' + 'ipt>') can be hard to read.

    var s = 'Hello <\/script>';
    

    For inline Javascript in HTML, you can use entities:

    <div onClick="alert('Hello &quot;>')">click me</div>
    

    Demo: http://jsfiddle.net/ThinkingStiff/67RZH/

    The method that works in both <script> blocks and inline Javascript is \uxxxx, where xxxx is the hexadecimal character code.

    • < – \u003c
    • > – \u003e
    • " – \u0022
    • ' – \u0027
    • \ – \u005c
    • & – \u0026

    Demo: http://jsfiddle.net/ThinkingStiff/Vz8n7/

    HTML:

    <div onClick="alert('Hello \u0022>')">click me</div>
    
    <script>
        var s = 'Hello \u003c/script\u003e';
    alert( s );
    </script>   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For our database development we have on one hand a full schema DDL script,
I have a Coda slider on one webpage (hand-crafted, not using the plugin, but
Google is failing me on this one. Let's say I have some ECMA script
In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and
I'm writing a 7 card poker hand evaluator as one of my pet projects.
I have a php script, which downloads, rescales and stores an image using the
I have a script that runs with root privileges, and I have a command
I have a pre-existing mysql database containing around 50 tables. Rather than hand code
I would like to real-hand experienced answer on this. Which one is faster? Writing
On the one hand mod_rewrite allows me to make userfriendly urls redirecting requests to

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.