Spencer Killen
Spencer Killen

Reputation: 25

Javascript won't escape Html string

Hey I've coded a piece of JavaScript that generates Html code and I'm trying to save the code as a string and then copy it over to a <textarea> but for some reason when I use escape() it brings up Uncaught SyntaxError: Unexpected token ILLEGAL in Google Chrome. and I cant figure out why, here's my code

            document.getElementById("share").value=escape('
<script src="main.js"></script>
<script src="event.js"></script>
<script src= "init.js"></script>
<script src= "util.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<canvas width="1280" height="500" id="header_canvas"     >Please Update your browser to view this page</canvas>
<canvas width="1280" height="500" id="sub_header_canvas" >Please Update your browser to view this page</canvas>

<script>
function submit()
{
    header_text                 ='+document.getElementById("title").value+';
    cover_time_pan              ='+document.getElementById("cover_turn_time").value+';
    cover_turn_pause_time       ='+document.getElementById("cover_turn_pause").value+';
    text_time_pan               ='+document.getElementById("text_turn_time").value+';
    text_turn_pause_time        ='+document.getElementById("text_turn_pause").value+';
    header_reverse              ='+document.getElementById("reverse").checked+';
    header_grad_percent     ='+document.getElementById("grad_height").value+';
    header_grad_color       ='+document.getElementById("grad_colour").value+';

    ct0.src                 ='+document.getElementById("ct0").value+';
    ct1.src                 ='+document.getElementById("ct1").value+';
    ct2.src                 ='+document.getElementById("ct2").value+';

    tt0.src                 ='+document.getElementById("tt0").value+';
    tt1.src                 ='+document.getElementById("tt1").value+';
    tt2.src                 ='+document.getElementById("tt2").value+';

     cover_textures[0]=ct0;
     cover_textures[1]=ct1;
     cover_textures[2]=ct2;
     text_textures[0]=tt0;
     text_textures[1]=tt1;
         text_textures[2]=tt2;


    resize_window();
}
</script>       
<script>
    init_all();
    submit();
</script>

');

Upvotes: 1

Views: 417

Answers (1)

hsemarap
hsemarap

Reputation: 227

The Problem is because javascript strings must be terminated before a newline character. The reason \n exists is to allow developers an easy way to put the newline character (ASCII: 10) into the string.

So for example, When you have a string which looks like this:

//Note terminating double quote is not there , similar to your code
var foo = "Bob 

Your code will have a syntax error at that point and cease to run.

If you wish to have a string in multiple lines, you must insert a backslash character '\' just before you terminate the line, like so:

//Correct way of writing code
var foo = "Bob \
is \
cool.";

However that string will not contain \n characters in the positions where the string was broken into separate lines. The only way to insert a newline into a string is to insert a character with a value of 10, the easiest way of which is the \n escape character.

var foo = "Bob\nis\ncool.";

Upvotes: 3

Related Questions