Tuesday 15 January 2013

How can I insert HTML text in Javascript the right way? -



How can I insert HTML text in Javascript the right way? -

i have code:

var p = document.createelement("p"); p.innerhtml = "welcome akecheta, web warrior social network. here you&#39;ll find blogger templates, freelancers area, question &amp;&nbsp;answers forum, create own blog , able interact other people.<br /> download free blogger templates, free blogger templates download, templates blogspot blogs, free download, free templates blogspot blogs, free templates blogspot blogs download."; document.body.appendchild(p);

but console returning error. checked out, looks html code breaking javascript code. error points straight line "welcome akecheta, web warrior social network. here you'll find blogger templates,"

i'm sorry if can't clarify question, don't know how set words. what's happening whole script not running because of html content, believe it's badly formatted.

edit 1

full code:

<script>// <![cdata[ var regex = new regexp("/testurl/$"); if(document.url.match(regex)) { var p = document.createelement("p"); p.innerhtml = "welcome akecheta, web warrior social network. here you\'ll find blogger templates, freelancers area, question \& answers forum. can create own blog interact other people.<br/><br/>download free blogger templates, free blogger templates download, templates blogspot blogs, free download, free templates blogspot blogs, free templates blogspot blogs download."; document.body.appendchild(p); } else { document.write(''); } // ]]></script>

edit 2

so tried this:

<script>// <![cdata[ var regex = new regexp("/free-blogger-templates/$"); if(document.url.match(regex)) { document.write('welcome akecheta, web warrior social network. here you\'ll find blogger templates, freelancers area, question \& answers forum. can create own blog interact other people.<br/>download free blogger templates, free blogger templates download, templates blogspot blogs, free download, free templates blogspot blogs, free templates blogspot blogs download.'); } else { document.write(''); } // ]]></script>

and worked. code beingness inserted on wysiwyg editor, whenever alter html editor visual editor , html editor, text code gets stripped.

final edit

here code working, code supposed hide/show text based on browser address, if want utilize it, alter /free-blogger-templates/ current page you'll run script, example, if you're running on yoursitedotcom/myfirstpage, alter /myfirstpage/$, $ means allow url until foward slash /, had utilize document.write, know it's not recomended, that's solution found, here is:

<script>// <![cdata[ var regex = new regexp("/free-blogger-templates/$"); if(document.url.match(regex)) { document.write('welcome akecheta, web warrior social network. here you\'ll find blogger templates, freelancers area, question \&amp; answers forum. can create own blog interact other people. download free blogger templates, free blogger templates download, templates blogspot blogs, free download, free templates blogspot blogs, free templates blogspot blogs download.'); } else { document.write(''); } // ]]></script>

the text had converted (thanks user @god (yeah, god =]), used http://www.web2generators.com/html/entities encode html text.

you needed escape of characters. seek this:

class="snippet-code-js lang-js prettyprint-override"> var p = document.createelement("p"); p.innerhtml = "welcome akecheta, web warrior social network. here you\'ll find blogger templates, freelancers area, question \& answers forum. can create own blog interact other people.<br/><br/>download free blogger templates, free blogger templates download, templates blogspot blogs, free download, free templates blogspot blogs, free templates blogspot blogs download."; document.body.appendchild(p);

edit: the entire code should jsfiddle.

javascript html

No comments:

Post a Comment