最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

insert html into div using javascript - Stack Overflow

programmeradmin5浏览0评论

Im having an issue when updating a div using javascript.

<script>
document.getElementById('advertisingBrandValues').innerHTML = "<p>The Mac is the most powerful   creative tool in the world, giving users unbridled potential to make everything from     bespoke family albums to industry-standard pop songs – iCreate is the key to unlocking that potential. Accessible, smart and authoritative, the iCreate brand thrives on the mantra ‘instruct, inform, inspire’, helping thousands of Mac users realise their creative ambitions.</p>";                   

</script>

<div id="advertisingBrandValues"></div>

I keep getting the following error:

unterminated string literal and it indicated that the error is with the very first P tag

Do i need to do somthing ot the HTML that i am passing into the innerHTML function?

Thanks in advance!

Im having an issue when updating a div using javascript.

<script>
document.getElementById('advertisingBrandValues').innerHTML = "<p>The Mac is the most powerful   creative tool in the world, giving users unbridled potential to make everything from     bespoke family albums to industry-standard pop songs – iCreate is the key to unlocking that potential. Accessible, smart and authoritative, the iCreate brand thrives on the mantra ‘instruct, inform, inspire’, helping thousands of Mac users realise their creative ambitions.</p>";                   

</script>

<div id="advertisingBrandValues"></div>

I keep getting the following error:

unterminated string literal and it indicated that the error is with the very first P tag

Do i need to do somthing ot the HTML that i am passing into the innerHTML function?

Thanks in advance!

Share Improve this question asked Sep 17, 2012 at 15:00 richelliotrichelliot 5862 gold badges11 silver badges30 bronze badges 1
  • No problems on Safari, other than some gibberish with the fancy symbols. – Jeffrey Sweeney Commented Sep 17, 2012 at 15:11
Add a ment  | 

3 Answers 3

Reset to default 5

Unterminated string literal usually means that you tried to do something like this:

var str = "Blah blah blah
  more blah that should be part of the string
  even more blah.";

You can't do this in JavaScript, you have to either end the string and append the next line:

var str = "Blah blah blah\n"
  +"more blah that should be part of the string\n"
  +"even more blah.";

Or you can escape the newlines (not sure if this is entirely standard):

var str = "Blah blah blah\
  more blah that should be part of the string\
  even more blah.";

Also note that the element you are trying to modify hasn't been defined yet. Either make sure the <div> you are trying to modify es before the <script> or defer the script or its contents (window.onload or similar)

The problem could be the fancy quotes you're using (‘ and ’). Try replacing them with '.

Inserting HTML into the page like this is generally not good practice due to the amount of parse errors and potential XSS vulnerabilities. Consider creating inner elements with document.createElement() and appending textual content in the child elements. It's a bit more verbose, but a lot more seamless to modify assuming the structure stays the same.

looks like the text has carriage returns in it.

maybe swap those for breaks before assigning, if you have the chance

stringWithReturnsIn.replace(/[\n\r]/g, '<br />');

the br if you want to keep the returns otherwise

stringWithReturnsIn.replace(/[\n\r]/g, ' ');
发布评论

评论列表(0)

  1. 暂无评论