开发者

Javascript Error "Missing ; before statement"

I am having a very weird error on this block:

<script type="text/javascript">
  var captions = new Array();
  captions[0] = "";
  capti开发者_如何学Goons[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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜