0

I have this url that I have to pass as a javascript string. I know that I need to escape the characters, but no matter what I do I can't seem to get it right?

 var v = "<a href="www.youbigboy.com/showthread.php?t=1847">www.youbigboy.com/showthread.php?t=1847</a>"; 
  • 4
    The syntax highlighter shows your error – John Conde Jul 24 '14 at 21:24
  • Surround it `'` instead of `"` or escape the `"` inside the string with `\"`. See [MDN: String Literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#String_literals) – p.s.w.g Jul 24 '14 at 21:25

3 Answers3

2

You are using double quotes inside a string declared with double quotes. You can:

  • Change the double quotes inside the string to single quotes
var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>";
  • Escape the double quotes inside the string with \
var v = "<a href=\"www.youbigboy.com/showthread.php?t=1847\">www.youbigboy.com/showthread.php?t=1847</a>";
  • Change the double quotes outside the string to single quotes
var v = '<a href="www.youbigboy.com/showthread.php?t=1847">www.youbigboy.com/showthread.php?t=1847</a>';
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
Forestrf
  • 364
  • 7
  • 13
1

you can use single quotes ' to differentiate internal quotes, or you can escape your internal quotes \"

The code becomes:

var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>"; 

or

var v = "<a href=\"www.youbigboy.com/showthread.php?t=1847\">www.youbigboy.com/showthread.php?t=1847</a>"; 
Patrick Gunderson
  • 3,263
  • 17
  • 28
0

maybe this

var url = "www.youbigboy.com/showthread.php?t=1847"; //or other dynamic url's
var sb = "<a href='" + url + "'>big boy url</a>";

source: is the + operator less performant than StringBuffer.append()

Community
  • 1
  • 1