Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javascript string inside a javascript string?

Inifinately nested javascript strings?

9:52 pm on Jun 28, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts: 100
votes: 0

Is it possible to put javascript strings inside javascript strings recursively?

I can get two levels deep by slashing the delimiting character of the inner set of javascript, but if i want to put a javascript string in that set i can't figure out how to double slash it.

Here's an example. I'll break out the "var html =" part on multiple lines and format it for readability.

In the example below, the "Hello World" part will alert properly, but the "I Fail" part won't work because the html2 variable is escaped with the same character - '. I've also tried using 'I Fail' but to no avail


function createHTML(str) {
// my function that dynamically creates new HTML

var html = '
<div class="test">

alert(&#039;Hello Wold&#039;) ;

var html2 = &#039;
<div class="test2">

alert(&#039;I Fail&#039;) ;

&#039; ;

createHTML(html2) ;


' ;

createHTML(html) ;

11:57 pm on June 28, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
votes: 743

You can also use both single and double quotes. As in html, they're interchangeable-- so long as they're properly paired-- and make things look less messy.

But that's visually messy. You also need to worry about structural messiness. It may come out "cleaner" if you say

var1 = dosomestuff
var2 = domorestuff(var1)
var3 = dostillmorestuff(var2)

instead of

endresult = dostillmorestuff(domorestuff(dosomestuff))
1:14 pm on June 29, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts: 100
votes: 0

I realize i could use double quotes also, but I still run into the same problem of having to double encode them as i get to the third layer.

i also thought about using multiple functions but due to the way i generate the files with the server side code it's not feasible.

I'm not concerned at all with how it looks because the PHP does it all for me. I just thought their might be a way to encode, then double encode quotes javascript kind of like in PHP when using htmlspecialchars() twice, which then if i use htmlspecialchars_decode() twice on that string, i get the original string.
4:27 pm on June 29, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts: 100
votes: 0

In case anyone is interested i found a solution:

you can use your programming language to create a function that does the following. my example is in PHP

function js_string($string, $delim = "'") {
// order of operations is important!

// add slashes before all existing slashes
$string = addcslashes($string, "\\") ; // the first slash for php

// add slashes before all delim chars
$string = addcslashes($string, $delim) ;

// add a slash to ending script tags
$string = str_replace('</script>', '<\/script>', $string) ;

return $string ;

then you can call this function on any string inside javascript strings then call the function on that string ...and so on

in the end it properly slashes all delim chars and script tags recursively