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

  • Home
  • SEARCH
  • 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 43403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:29:23+00:00 2026-05-10T15:29:23+00:00

Maybe I’m just thinking about this too hard, but I’m having a problem figuring

  • 0

Maybe I’m just thinking about this too hard, but I’m having a problem figuring out what escaping to use on a string in some JavaScript code inside a link’s onClick handler. Example:

<a href='#' onclick='SelectSurveyItem('<%itemid%>', '<%itemname%>'); return false;'>Select</a> 

The <%itemid%> and <%itemname%> are where template substitution occurs. My problem is that the item name can contain any character, including single and double quotes. Currently, if it contains single quotes it breaks the JavaScript code.

My first thought was to use the template language’s function to JavaScript-escape the item name, which just escapes the quotes. That will not fix the case of the string containing double quotes which breaks the HTML of the link. How is this problem normally addressed? Do I need to HTML-escape the entire onClick handler?

If so, that would look really strange since the template language’s escape function for that would also HTMLify the parentheses, quotes, and semicolons…

This link is being generated for every result in a search results page, so creating a separate method inside a JavaScript tag is not possible, because I’d need to generate one per result.

Also, I’m using a templating engine that was home-grown at the company I work for, so toolkit-specific solutions will be of no use to me.

  • 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. 2026-05-10T15:29:24+00:00Added an answer on May 10, 2026 at 3:29 pm

    In JavaScript you can encode single quotes as ‘\x27’ and double quotes as ‘\x22’. Therefore, with this method you can, once you’re inside the (double or single) quotes of a JavaScript string literal, use the \x27 \x22 with impunity without fear of any embedded quotes ‘breaking out’ of your string.

    \xXX is for chars < 127, and \uXXXX for Unicode, so armed with this knowledge you can create a robust JSEncode function for all characters that are out of the usual whitelist.

    For example,

    <a href='#' onclick='SelectSurveyItem('<% JSEncode(itemid) %>', '<% JSEncode(itemname) %>'); return false;'>Select</a> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is an easier way: session_name. Prior to calling session_start();… May 11, 2026 at 5:04 pm
  • Editorial Team
    Editorial Team added an answer I find the App Store to be the best, despite… May 11, 2026 at 5:04 pm
  • Editorial Team
    Editorial Team added an answer How about: public static T Bar2<T>(Func<IDataReader,T> func) where T :… May 11, 2026 at 5:03 pm

Related Questions

Maybe I just don't know .NET well enough yet, but I have yet to
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
Maybe I live in a bubble, or am just too new, but I was
Maybe I'm not looking in the right places but I'd like to use SQL
Maybe i cant do what i want? I want to have 1 thread do

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.