views:

2011

answers:

2

I am having problem with escaping the single and double quotes inside the hrefs javascript function -

I have this javascript code inside href - its like -

<a href = "javascript:myFunc("fileDir/fileName.doc" , true)"> click this </a>

Now, since double quotes inside double quote is not valid - I need to escape the inner double quotes for it to be treated as part of the string - so, i need to do this -

<a href = "javascript:myFunc(\"fileDir/fileName.doc\" , true)"> click this </a>

The problem is, even the above code is not working - the javascript is getting truncated at -- myFunc(

I tried with single quote variation too - but even that doestnt seem to work - (meaning that if I have a single quote inside my string literal then the code gets truncated -)

This is what I did with single quote -

<a href = 'javascript:myFunc("fileDir/fileName.doc" , true)'> click this </a>

this works - BUT If I have a single quote inside the string then the code gets truncated in the same way as that of double quotes one.

Thanks.

+9  A: 

Using backslashes to escape quotes is how it works in javascript, but you're not actually writing javascript there: you're writing HTML.You can do it by using the HTML escaping method: character entities.

&quot;  // "
&#39;   // '

For example:

<a href="javascript: alert('John O&#39;Brien says &quot;Hi!&quot');">...</a>
nickf
A: 

Normally, this kind of code is working without problems:

<a href="#" onclick="myFunc('...')">Click this</a>

With this code, do you have any problem?

romaintaz
nickf