最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript code not working in JSF xhtml page - Stack Overflow

matteradmin5PV0评论

Here is the running code on fiddle
You will see that It is working perfectly fine here but when I run this code in eclipse using glassfish server 3.2.1 in a xhtml page then it gives this error

javax.servlet.ServletException: Error Parsing /MasterPage/MiDASMaster.xhtml: Error Traced[line: 135] Open quote is expected for attribute "{1}" associated with an  element type  "class".


Here is the code of xhtml page(exactly same like fiddle) I tried it on Jsbin as well

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    ".dtd">

<html>
<head>
<style type="text/css">
.highlight {
    background:yellow;
}
.removeHighlight {
    background:green;
}

</style>
<script type="text/javascript" src="../Scripts/jquery-1.8.3.js"/>
</head>
<body>

<script type="text/javascript">

            function test(){
        alert(document.getElementById("divId"));
         var regex = new RegExp('this',"gi");
         document.getElementById("divId").innerHTML     
         =document.getElementById("divId").innerHTML.replace(regex, function(matched) 
        {
            return '<span class=\'highlight\'>' + matched + '</span>';
        });


    }



</script>

<div id="divId">

    This is the text This is the text This is the text This is the text 
    This is the text This is the text This is the the text
</div>

..

Here is the running code on fiddle
You will see that It is working perfectly fine here but when I run this code in eclipse using glassfish server 3.2.1 in a xhtml page then it gives this error

javax.servlet.ServletException: Error Parsing /MasterPage/MiDASMaster.xhtml: Error Traced[line: 135] Open quote is expected for attribute "{1}" associated with an  element type  "class".


Here is the code of xhtml page(exactly same like fiddle) I tried it on Jsbin as well

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<style type="text/css">
.highlight {
    background:yellow;
}
.removeHighlight {
    background:green;
}

</style>
<script type="text/javascript" src="../Scripts/jquery-1.8.3.js"/>
</head>
<body>

<script type="text/javascript">

            function test(){
        alert(document.getElementById("divId"));
         var regex = new RegExp('this',"gi");
         document.getElementById("divId").innerHTML     
         =document.getElementById("divId").innerHTML.replace(regex, function(matched) 
        {
            return '<span class=\'highlight\'>' + matched + '</span>';
        });


    }



</script>

<div id="divId">

    This is the text This is the text This is the text This is the text 
    This is the text This is the text This is the the text
</div>

..

Share Improve this question asked Jul 3, 2013 at 8:44 vikiviki 651 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Your XHTML is probably malformed.

Put your Javascript code into a CDATA section.

<script type="text/javascript">
    <![CDATA[
        alert("Your javascript here");
    ]]>
</script>
Post a comment

comment list (0)

  1. No comments so far