homepage Welcome to WebmasterWorld Guest from 54.166.128.254
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript string inside a javascript string?
Inifinately nested javascript strings?
noyearzero

5+ Year Member



 
Msg#: 4332264 posted 9:52 pm on Jun 28, 2011 (gmt 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

<script>

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

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

<script>
alert(&#039;Hello Wold&#039;) ;

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

<script>
alert(&#039;I Fail&#039;) ;
</script>

<div>
&#039; ;

createHTML(html2) ;

</script>

</div>
' ;

createHTML(html) ;

</script>

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4332264 posted 11:57 pm on Jun 28, 2011 (gmt 0)

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))

noyearzero

5+ Year Member



 
Msg#: 4332264 posted 1:14 pm on Jun 29, 2011 (gmt 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.

noyearzero

5+ Year Member



 
Msg#: 4332264 posted 4:27 pm on Jun 29, 2011 (gmt 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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved