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

Javascript Error "Missing ; before statement" - Stack Overflow

programmeradmin1浏览0评论

I am having a very weird error on this block:

<script type="text/javascript">
  var captions = new Array();
  captions[0] = "";
  captions[1] = '<div class="cap-desc"><h3>No Win - No Fee</h3><span class="captionSubhead">Performance Guarantee</span><br /><span style="color:#636363; font-size:13px;">Ask Today. We are very confident<br />you'll be impressed with our results! </span></div><br /><a href="http://#" class="rmore" target="_blank">read more</a>';
</script>

The error tells me that I am missing a ';' before captions[1] but I am pretty sure that captions[0] has a semi-colon!

The whole site is here: ;a/

It has 5 captions now, but same error nonetheless. I tried changing the contents of the caption in question. In this case, it's captions[1] and it works. What is wrong with the caption? I am sure I am closing all tags and quotes and escaping \n to

I am having a very weird error on this block:

<script type="text/javascript">
  var captions = new Array();
  captions[0] = "";
  captions[1] = '<div class="cap-desc"><h3>No Win - No Fee</h3><span class="captionSubhead">Performance Guarantee</span><br /><span style="color:#636363; font-size:13px;">Ask Today. We are very confident<br />you'll be impressed with our results! </span></div><br /><a href="http://#" class="rmore" target="_blank">read more</a>';
</script>

The error tells me that I am missing a ';' before captions[1] but I am pretty sure that captions[0] has a semi-colon!

The whole site is here: http://katron.sourcefit.com/cms02/a&a/

It has 5 captions now, but same error nonetheless. I tried changing the contents of the caption in question. In this case, it's captions[1] and it works. What is wrong with the caption? I am sure I am closing all tags and quotes and escaping \n to

Share Improve this question asked Aug 3, 2011 at 6:30 Rolando CruzRolando Cruz 2,7841 gold badge17 silver badges24 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 7

You are not escaping a single quote properly...

captions[1] = '<div class="cap-desc"><h3>No Win - No Fee</h3><span class="captionSubhead">Performance Guarantee</span><br /><span style="color:#636363; font-size:13px;">Ask Today. We are very confident<br />you\'ll be impressed with our results! </span></div><br /><a href="http://#" class="rmore" target="_blank">read more</a>';

This just escapes the apostrophe in

you'll

The problem is you'll has an apostrophe and ends the code block. Make sure to escape it.

Found this question when I was googling the error.

My solution is not relevant to the original post but may help someone anyway.

I had forgotten the word function after a declaration:

Foo.bar(evt) {
    ...
}

should have been

Foo.bar = function(evt) {
    ...
}

Took me a little while to find this bug, so hopefully it helps someone.

The same error can occur if you are trying to add a property to an existing object prefixing with a var. For example

var existingObject = {};

var existingObject.property1 = {}; //will throw the same error

The solution is:

var existingObject = {};

existingObject.property1 = {}; //without prefixing with var

That is because of single quote in last line at confident
you'll be impressed
repalce it with \' and it think that will fix it

you'll be impressed with our results!

That apostrophe (') in you'll is ending your string. So the javascript engine is looking for a semicolon there.

replace with \' to escape it

Check "you'll be impressed with our results! " in your captions[1] it has ' character which does not have escape character.

发布评论

评论列表(0)

  1. 暂无评论