未捕获的SyntaxError:意外的标记ILLEGAL

时间:2014-10-03 08:12:34

标签: javascript liferay-velocity

我使用liferay 5.2

我想显示与liferay当前语言相关的相同消息

此消息的代码位于velocity lnaguage

这是源代码navigation.vm

<div id="navigation" class="sort-pages modify-pages">
    <ul>
        #foreach ($nav_item in $nav_items)

            #if (($nav_item.getTitle() == 'register_correspondence') )
                    #if ($nav_item.hasChildren())
                        #foreach ($nav_child in $nav_item.getChildren())
                          <li >
                            <a href="$nav_child.getURL()" $nav_child.getTarget()>
                                <span >$nav_child.getName()</span>
                            </a>
                          </li>
                        #end
                    #end

            #end

            #if (($nav_item.getTitle() == 'reports') )      
              #if ($nav_item.isSelected())
                #set ($nav_item_class = "selected")
            #else
                #set ($nav_item_class = "")
            #end

            <li>

                #if (($nav_item.getTitle() == 'logout'))
                    <a href="$sign_out_url" $nav_item.getTarget()>
                        <span class="$nav_item.getTitle()">$nav_item.getName()</span>
                    </a>
                #else
                    <a href="$nav_item.getURL()" $nav_item.getTarget()>
                        <span class="$nav_item.getTitle()">$nav_item.getName()</span>
                    </a>
                #end

                #if ($nav_item.hasChildren())
                    <ul class="child-menu">
                        #foreach ($nav_child in $nav_item.getChildren())
                            #if ($nav_child.isSelected())
                                #set ($nav_child_class = "selected")
                            #else
                                #set ($nav_child_class = "")
                            #end

                            <li class="$nav_child_class">
                                <a href="$nav_child.getURL()" $nav_child.getTarget()>$nav_child.getName()</a>
                            </li>
                        #end
                    </ul>

                #end

            </li>#end
            #end
            #if ($show_sign_out)
            <li class="$nav_child_class">
                <A HREF="$sign_out_url" onCLick="return confirm(' Are you sure you want to log out ')"> <font color="FF0000">
                Logout
                </font>
                </A>
            </li>

        #end
    </ul>
</div>

我的信息是在这个应答中

<li class="$nav_child_class">
                <A HREF="$sign_out_url" onCLick="return confirm(' Are you sure you want to log out ')"> <font color="FF0000">
                Logout
                </font>
                </A>
            </li>

我想将这条消息与现有的liferay语言相提并论

表示当语言为法语时,该消息将以法语显示

我尝试使用此代码时没有成功:

<div id="navigation" class="sort-pages modify-pages">
    <ul>
        #foreach ($nav_item in $nav_items)

            #if (($nav_item.getTitle() == 'register_correspondence') )
                    #if ($nav_item.hasChildren())
                        #foreach ($nav_child in $nav_item.getChildren())
                          <li >
                            <a href="$nav_child.getURL()" $nav_child.getTarget()>
                                <span >$nav_child.getName()</span>
                            </a>
                          </li>
                        #end
                    #end

            #end

            #if (($nav_item.getTitle() == 'reports') )      
              #if ($nav_item.isSelected())
                #set ($nav_item_class = "selected")
            #else
                #set ($nav_item_class = "")
            #end

            <li>

                #if (($nav_item.getTitle() == 'logout'))
                    <a href="$sign_out_url" $nav_item.getTarget()>
                        <span class="$nav_item.getTitle()">$nav_item.getName()</span>
                    </a>
                #else
                    <a href="$nav_item.getURL()" $nav_item.getTarget()>
                        <span class="$nav_item.getTitle()">$nav_item.getName()</span>
                    </a>
                #end

                #if ($nav_item.hasChildren())
                    <ul class="child-menu">
                        #foreach ($nav_child in $nav_item.getChildren())
                            #if ($nav_child.isSelected())
                                #set ($nav_child_class = "selected")
                            #else
                                #set ($nav_child_class = "")
                            #end

                            <li class="$nav_child_class">
                                <a href="$nav_child.getURL()" $nav_child.getTarget()>$nav_child.getName()</a>
                            </li>
                        #end
                    </ul>

                #end

            </li>#end
            #end
            #if ($show_sign_out)

    <script type="text/javascript">

        var varlang = "$themeDisplay.getLocale()";

        if( varlang == 'en_US') {

        document.write('<li class="$nav_child_class">
                <A HREF="$sign_out_url" onCLick="return confirm(' Are you sure you want to log out ')"> <font color="FF0000">
                Logout
                </font>
                </A>
            </li>');

        }
        else
        {

        document.write('<li class="$nav_child_class">
                <A HREF="$sign_out_url" onCLick="return confirm(' Etes-vous sûr que vous voulez vous déconnecter ')"> <font color="FF0000">
                Déconnexion
                </font>
                </A>
            </li>');

        }

        </script>


        #end
    </ul>
</div>

我在javascript代码中进行测试我添加了这行 var varlang =&#34; $ themeDisplay.getLocale()&#34;;

但是当我测试时我有这个错误:

Uncaught SyntaxError: Unexpected token ILLEGAL 

我只更改了本节中的代码

#if($ show_sign_out)

1 个答案:

答案 0 :(得分:0)

您的脚本标记内的错误是javascript错误。

这是无效的

document.write('<li class="$nav_child_class">
        <A HREF="$sign_out_url" onCLick="return confirm(' Are you sure you want to log out ')"> <font color="FF0000">
        Logout
        </font>
        </A>
    </li>');

有两个原因。

第一个问题是字符串不能跨越多行。

第二个问题是你的字符串中有单引号用单引号开头,你必须转义内部单引号。

您应该将其重写为

document.write('<li class="$nav_child_class"><A HREF="$sign_out_url" onCLick="return confirm(\' Are you sure you want to log out \')"> <font color="FF0000">Logout</font></A></li>');

或至少作为

document.write('<li class="$nav_child_class">' +
        '<A HREF="$sign_out_url" onCLick="return confirm(\' Are you sure you want to log out \')"> <font color="FF0000">' +
        'Logout' +
        '</font>' +
        '</A>' +
    '</li>');

但是你为什么要用脚本标签呢?

我宁愿有类似

的东西
#if ($show_sign_out)

    #if ($themeDisplay.getLocale() == "en_US")
        #set($logoutLabel = "Logout")
        #set($logoutConfirm = "Are you sure you want to log out")
    #else
        #set($logoutLabel = "Déconnexion")
        #set($logoutConfirm = "Etes-vous sur que vous voulez vous déconnecter")
    #end    

    <li class="$nav_child_class">
        <A HREF="$sign_out_url" onCLick="return confirm('$logoutConfirm')"> <font color="FF0000">
            $logoutLabel
        </font></A>
    </li>

#end 
相关问题