1

I have a javascript method call with a string parameter. In the string text sometimes contains html character references, e.g. ' I am getting an unexpected identifier error. If I have the character reference as " then it works fine. Not sure why that is. Below is a code snippet of what I am trying to do. Actual method is much longer and trying to do something different than what I show here, but this snippet should be able to reproduce the error.

<script>
function unescapeHTML(html) {
  var htmlNode = document.createElement("div");
  htmlNode.innerHTML = html;
  if(htmlNode.innerText)
    alert htmlNode.innerText; // IE
  else 
    alert htmlNode.textContent; // FF

}
</script>
<a class="as_Glossary" onmouseover="unescapeHTML('The manufacturer&#39;s sales in dollars to all purchasers in the United States excluding certain exemptions for a specific drug in a single calendar quarter divided by the total number of units of the drug sold by the manufacturer in that quarter'); return true;" onmouseout="hideGlossary(); return true;">Test</a>

When I mouseover I get the error

4
  • There is no charachter between 9 and semi-colon right? Commented Jan 8, 2010 at 18:52
  • 1
    could you maybe post the JavaScript source or at least piecs of it? This increases the time to get high-quality answers considerably ;) Commented Jan 8, 2010 at 18:56
  • Thanks Juri, I added some snippet to demonstrate the issue Commented Jan 8, 2010 at 19:18
  • @Emrah no there isn't any character between 9 and semi-colon. Commented Jan 8, 2010 at 19:21

2 Answers 2

2

The issue is that your &#39; is being converted to a ' before the JavaScript is evaluated. So, JavaScript sees the following (wrapped for readability):

unescapeHTML('The manufacturer's sales in dollars to all purchasers in 
the United States excluding certain exemptions for a specific drug in a 
single calendar quarter divided by the total number of units of the drug 
sold by the manufacturer in that quarter'); 
return true;

Notice how the string ends after manufacturer, and the rest is treaded as code, with an extra unmatched close quote '. You need to prefix the ' in manufacturer's with a backslash in order for the string to be properly quoted in JavaScript:

a class="as_Glossary" onmouseover="unescapeHTML('The manufacturer\&#39;s sales...

You also need parentheses in your alert expressions:

function unescapeHTML(html) {
  var htmlNode = document.createElement("div");
  htmlNode.innerHTML = html;
  if(htmlNode.innerText)
    alert(htmlNode.innerText); // IE
  else 
    alert(htmlNode.textContent); // FF
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need a semi-colon after that character reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.