控制台报告的奇怪的javascript语法错误

时间:2015-01-30 18:07:41

标签: javascript syntax-error

我使用php生成缩略图图像和标题的表格。一个简单的JavaScript控制在主图像区域中显示这些缩略图及其标题。

第二行中的最后一个图像(3-08.jpg)在参数列表后给出错误" SyntaxError:missing)。"

奇怪的是,那里有,和所有其他人一样。并且,由于此列表是自动生成的,因此每个缩略图和代码的代码没有任何区别。标题。

这是脚本:

function changeImage(filename, imagetitle)
{
   document.getElementById('mainImage').src = filename;
   document.getElementById('caption').innerHTML = imagetitle;
}

这是生成的html:

                        <tr align="center" valign="bottom" class="thumbnails">
                      <td>                <a href="javascript:changeImage('adminNew/images/3-01.jpg','Attractive Tamarac Trail home with comfortable living spaces and a beautifully landscaped yard.')"><img src="adminNew/images/3-01.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-02.jpg','The tiled entry is a great spot to shed those boots and coats - and a pretty spot to relax, too.')"><img src="adminNew/images/3-02.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-03.jpg','Beautiful maple flooring and exquisite detailing add a lot of character to this home.')"><img src="adminNew/images/3-03.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-04.jpg','Counter seating will allow your family to grab a quick meal while visiting with the cook.')"><img src="adminNew/images/3-04.jpg"> </a>
           </td> 
                                            </tr>
                    <tr align="center" valign="bottom" class="thumbnails">
                      <td>                <a href="javascript:changeImage('adminNew/images/3-05.jpg','Dining is delightful in this area full of windows.')"><img src="adminNew/images/3-05.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-06.jpg','The main floor is open and inviting.')"><img src="adminNew/images/3-06.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-07.jpg','A cozy and carpeted living room makes a wonderful gathering place for family and friends.')"><img src="adminNew/images/3-07.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-08.jpg','The lower level has a very nice family room, two bedrooms, and a full bath.  But, wait... there's more!')"><img src="adminNew/images/3-08.jpg"> </a>
           </td> 
                                            </tr>
                    <tr align="center" valign="bottom" class="thumbnails">
                      <td>                <a href="javascript:changeImage('adminNew/images/3-09.jpg','Formerly used as a stained glass studio, the artist in your family will enjoy making this large space their own.')"><img src="adminNew/images/3-09.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-10.jpg','This spacious wood shop, complete with outside entry, will delight any woodworker.')"><img src="adminNew/images/3-10.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-11.jpg','The deck will be a popular spot during the warmer months. ')"><img src="adminNew/images/3-11.jpg"> </a>
           </td> 
                                  <td>                <a href="javascript:changeImage('adminNew/images/3-12.jpg','We all need a little more storage!')"><img src="adminNew/images/3-12.jpg"> </a>
           </td> 
                                            </tr>

任何人都知道为什么会出现这种错误?

非常感谢提前!

1 个答案:

答案 0 :(得分:2)

您的描述包含单引号并包含在单引号中,因此字符串以&#34; wait ... there&#34;,然后是(to JS)无意义s more!'结束。

确保在构建页面之前转义描述字符串,否则您将遇到很多这样的问题。你需要(至少)通过在它们前面加上反斜杠(\')来转义所有引号,包括单引号和双引号。你也应该以同样的方式逃避反斜杠。

PHP似乎为此提供了一个函数:addslashes

请记住,如果这些描述来自用户输入,则需要非常小心地清理和转义任何可能使其编写浏览器脚本的内容,否则您将容易受到各种XSS的攻击