0

I have this code:

 var p = document.createElement("p"); 

p.innerHTML = "Welcome to Akecheta, the web warrior social network. Here you&#39;ll find blogger templates, a freelancers area, Question &amp;&nbsp;Answers Forum, create your own blog and will be able to interact with other people.<br />
Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download."; 
document.body.appendChild(p); 

But the console is returning an error. I checked it out, it looks like the html code is breaking the javascript code. The error points directly to this line "Welcome to Akecheta, the web warrior social network. Here you'll find blogger templates,"

I'm sorry if I can't clarify my question, I don't know how to put that into words. What's happening is that the whole script is not running because of that html content, I 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 to Akecheta, the web warrior social network. Here you\'ll find blogger templates, a freelancers area, Question \& Answers Forum.  You can even create your own blog to interact with other people.<br/><br/>Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download."; 
document.body.appendChild(p); 

}
else {
document.write('');
  }
// ]]></script>

EDIT 2

So I tried this:

<script>// <![CDATA[
var regex = new RegExp("/free-blogger-templates/$");
if(document.URL.match(regex))
{
document.write('Welcome to Akecheta, the web warrior social network. Here you\'ll find blogger templates, a freelancers area, Question \& Answers Forum.  You can even create your own blog to interact with other people.<br/>Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.');
}
else {
document.write('');
  }
// ]]></script>

And it fully worked. But this code is being inserted on a WYSIWYG editor, so whenever I change the html editor to the visual editor and get back to the html editor, the text code gets stripped.

FINAL EDIT

Here is my code fully working, this code is supposed to hide/show a text based on the browser address, if you want to use it, just change /free-blogger-templates/ to the current page where you'll run your script, for example, if you're running on yoursitedotcom/myfirstpage, change it to /myfirstpage/$, the $ means it will just allow the url until the foward slash /, I had to use document.write, I know it's not recomended, but that's the only solution I found, here it is:

<script>// <![CDATA[
var regex = new RegExp("/free-blogger-templates/$"); if(document.URL.match(regex)) { document.write('Welcome to Akecheta, the web warrior social network. Here you\'ll find blogger templates, a freelancers area, Question \&amp; Answers Forum. You can even create your own blog to interact with other people. Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.'); } else { document.write('');   }
// ]]></script>

The text had to be properly converted (Thanks to the user @God is good (Yeah, God is really good =]), then I used http://www.web2generators.com/html/entities to encode the HTML text.

2
  • I did some debugging, and the problem is in your RegExp. Commented Nov 1, 2014 at 3:44
  • /testurl/ is not a regexp, and it's canceling your code. Commented Nov 1, 2014 at 3:45

3 Answers 3

3

Strings in javascript can't have line breaks in them. Here is one alternative.

p.innerHTML = "Welcome to Akecheta, the web warrior social network. Here you&#39;ll find blogger templates, a freelancers area, Question &amp;&nbsp;Answers Forum, create your own blog and will be able to interact with other people.<br />" +
"Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download."; 
document.body.appendChild(p); 
Sign up to request clarification or add additional context in comments.

3 Comments

Uncaught TypeError: Cannot read property 'appendChild' of null (index):15(anonymous function)
Removing the line breaks did the job perfectly, this might be another problem related to the mechanism of Blogger templates, which are quite confusing. Would you like to share a link to the blog/site where this is happening ?
It's on localhost and on the website. I'm running the text on localhost due to speed reasons. Here the page akecheta.com/free-blogger-templates
1

You Need To Create Text Node Than Append into P tag and than append into the dom
try this

var p = document.createElement("p"); 
var text = document.createTextNode("Welcome to Akecheta, the web warrior social network. Here you&#39;ll find blogger templates, a freelancers area, Question &amp;&nbsp;Answers Forum, create your own blog and will be able to interact with other people.<br />
Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download.");

p.appendChild(text); 
document.body.appendChild(p);

See DEMO HERE

2 Comments

I also created a fiddle and it works, I'm wondering why it is not working on the localhost, I even deleted the url, leaving on the /$
Either createTextNode() document method or textContent property of the element (if available). Both will take care of properly encoding the string. innerHTML shouldn't be used for text, unless you're actually parsing and inserting a well formed HTML string.
0

You needed to escape some of your characters. Try this:

 var p = document.createElement("p"); 

p.innerHTML = "Welcome to Akecheta, the web warrior social network. Here you\'ll find blogger templates, a freelancers area, Question \& Answers Forum.  You can even create your own blog to interact with other people.<br/><br/>Download Free Blogger Templates, Free Blogger Templates Download, Templates for blogspot blogs, Free download, Free templates for blogspot blogs, Free templates for blogspot blogs download."; 
document.body.appendChild(p); 

EDIT: The entire code should look like this JSFIDDLE.

5 Comments

Why am I getting this error? Uncaught TypeError: Cannot read property 'appendChild' of null (index):15(anonymous function) –
Not sure . . . The above code works for me. Do you have other code on your page?
I'm thinking there is an error in surrounding code . . .
I saw your code, but it accepts the text at any page, right? the RegExp is there to make sure that the text displays only on a single page of the site, if the url is different from that page, the text will hide.
Yes, you could do that with RegExp, but the RegExp that you had blocked all pages. Just put this code on the page that you want it on. Or, I can help you create a different RegExp.