Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape Quotes In HTML5 Data Attribute Using Javascript

I'm using jQuery's .data() to work with custom HTML5 data attributes where the value of the attribute needs to be able to contain both single quotes and double quotes:

<p class="example" data-example="She said "WTF" on last night's show."> 

I know using character codes like &quot; in the data attribute value could make the above work, but I can't always control how the values are inputted. Plus, I need to be able to use HTML tags in the markup, like this:

<p class="example" data-example=" She said "<abbr title="What The F***">WTF</abbr>" on last night's show. "> 

If some form of .replace() is the answer, then it needs to be done before the value is read by .data()—maybe by applying it across the entire <body>?

Normal backslash escaping like <abbr title="te\'st">WTF</abbr> doesn't work either.

Ideally this would have the flexibility to work w/ both:

data-example="..." and data-example='...'

But if it's only possible one way then I could at least roll with that. Ideas?

Update - some more context:

I'm working on this for responsejs.com. An actual application of this might be to only load a sidebar for browsers above a certain width (and have this handled in the browser rather than PHP). In the case of WordPress for example, the sidebar could contain widgets, images, etc. The quotes within PHP tags are a non-issue b/c they are parsed into HTML before they get to the browser. Example:

<aside id="primary" class="sidebar"           data-oweb='               <?php dynamic_sidebar( 'primary' ); ?>          '     >      optional default markup for mobile and no-js browsers here  </aside> 
like image 269
ryanve Avatar asked Aug 31 '11 16:08

ryanve


People also ask

How do you escape quotes in JavaScript?

To escape a single or double quote in a string, use a backslash \ character before each single or double quote in the contents of the string, e.g. 'that\'s it' .

How do you escape quotes in HTML?

JavaScript Strings Escaping quotes Quotes in HTML strings can also be represented using &apos; (or &#39; ) as a single quote and &quot; ( or &#34; ) as double quotes. Note: The use of &apos; and &quot; will not overwrite double quotes that browsers can automatically place on attribute quotes.


1 Answers

There is no way around it, you have to escape the values properly, or the HTML can't be parsed properly. You can't use Javascript to correct the code after it is parsed, because then it has already failed.

Your example with proper HTML encoding would be:

<p class="example" data-example="She said &quot;&lt;abbr title=&quot;What The F***&quot;&gt;WTF&lt;/abbr&gt;&quot; on last night's show."> 

You can't use backslash to escape characters, because it's not Javascript code. You use HTML entities to escape characters in HTML code.

If you can't control how the data is input, then you are screwed. You simply have to find a way to take control over it.

like image 185
Guffa Avatar answered Sep 20 '22 18:09

Guffa