jjj
jjj

Reputation: 245

Line change with javascript

I need to create script tag and javascript code dynamically.

<script>
alert(0);
alert(1);
</script>

Supposedly, I want to create the script above.

var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref[(fileref.innerText===undefined?"textContent":"innerText")] = "<script>alert(0);alert(1);</script>";

How do I mimic the line change (i.e. after alerts there are line changes)? I've tried \n but it doesn't work (br doesn't work either).

You may say it makes no difference, but I must make the line change because I'm trying to use LinkedIn API which provides a unique js implementation that require the line change.

Upvotes: 1

Views: 204

Answers (2)

Ben A. Hilleli
Ben A. Hilleli

Reputation: 606

Try "&#10;" This is the character code for newline.

Upvotes: 1

rajesh kakawat
rajesh kakawat

Reputation: 10906

try something like this,Fiddle

    var fileref=document.createElement('script');
    fileref.type = 'text/javascript';

    var code = 'alert(0);alert(1);';
    fileref.appendChild(document.createTextNode(code));
    document.body.appendChild(fileref);

Upvotes: 0

Related Questions