开发者

Strange issue with ">" and "<" being converted to "&lt;" and "&gt;" in javascript in my jsf app

I have some javascript that i'm trying to use in a jsf app. I've narrowed it down so that if I take out the lines below, things work fine, but when I have these lines in, I get an error in the chrome console which says "unexpected ;" and it shows the first line as if(maxdays &gt; 1000) {

Why is it converting the greater than symbol to &gt;?

if(maxdays > 1000) {
    maxdays = 1000;
}

EDIT: Here's the entire JSF page.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">


<h:head>


    <title>Protocol Dashboard</title>
    <link type="text/css" rel="stylesheet" href="../css/styles.css" />
    <script type="text/javascript" src="../js/jquery-1.5.2.js"></script>
    <script type="text/javascript" src="../js/highcharts.src.js"></scrip开发者_如何学运维t>
    <script type="text/javascript">
        $(document).ready(
                function() {
                    //var chartData;
                    //var goodData;
                    var chart;
                    var studyType;
                    var categories;
                    var maxdays;
                    //var chart = new Highcharts.Chart({
                    var options = {

                        chart : {
                            renderTo : 'container',
                            type : 'bar'
                        },

                        title : {
                            text : 'Gantt module development plan'
                        },
                        subtitle: {
                            text: studyType
                        },
                        xAxis : {
                            categories : [ 'Planning', 'Development',
                                    'Testing', 'Documentation' ]
                        },

                        yAxis : {
                            type : 'datetime',
                            min : Date.UTC(2012, 0, 1),
                            labels : {
                                formatter : function() {
                                    return Highcharts.dateFormat('%m/%d/%Y',
                                            this.value);
                                }
                            }

                        },

                        tooltip : {
                            formatter : function() {
                                var point = this.point;
                                return '<b>'
                                        + point.category
                                        + '</b><br/>'
                                        + Highcharts.dateFormat('%b %e, %Y',
                                                point.low)
                                        + ' - '
                                        + Highcharts.dateFormat('%b %e, %Y',
                                                point.y);
                            }
                        },

                        series : [ {
                            data : [ {
                                low : Date.UTC(2012, 0, 1),
                                y : Date.UTC(2012, 0, 15)
                            }, {
                                low : Date.UTC(2012, 0, 10),
                                y : Date.UTC(2012, 4, 28)
                            }, {
                                low : Date.UTC(2012, 3, 15),
                                y : Date.UTC(2012, 4, 28)
                            }, {
                                low : Date.UTC(2012, 4, 15),
                                y : Date.UTC(2012, 4, 28)
                            } ]
                        }, {
                            data : [ {
                                low : Date.UTC(2012, 0, 1),
                                y : Date.UTC(2012, 1, 15)
                            }, {
                                low : Date.UTC(2012, 0, 10),
                                y : Date.UTC(2012, 3, 15)
                            }, {
                                low : Date.UTC(2012, 3, 15),
                                y : Date.UTC(2012, 6, 14)
                            }, {
                                low : Date.UTC(2012, 4, 15),
                                y : Date.UTC(2012, 4, 25)
                            } ]
                        } ]

                    };

                    function loadData() {

                        //options.series.length = 0;
                        //options.xAxis.categories.length = 0;

                        $('#hiddenData').val('09-034,99|10-053,194');
                        $('#hiddenCategories').val('09-034|10-053');
                        $('#clickedMenu').val('P2C');
                        $('#hiddenMaxDays').val('194');



                        var chartData = $('#hiddenData').val();
                        console.log('hiddenData = '+chartData);

                        categories = $('#hiddenCategories').val();
                        console.log('hiddenCategories = '+categories);

                        studyType = $('#clickedMenu').val();
                        console.log('clickedMenu = '+studyType);

                        maxdays = $('#hiddenMaxDays').val();
                        console.log('hiddenMaxDays = '+maxdays);
                        console.log('maxdays = '+maxdays);



                        //options.yAxis.max = maxdays;

                        options.subtitle.text = studyType;

                        var goodData = chartData.split('|');
                        //console.log('goodData = '+goodData);

                        //var goodCategories = categories.split(",");
                        //console.log('goodCategories = '+goodCategories);
                        /*
                        var series = {

                            data : []
                        };

                        var cat = {
                            categories : []
                        };

                        try {
                            $.each(goodData, function(index, value) {
                                var goodData2 = value.split(",");
                                //series.name = goodData2[0];
                                series.data.push(parseFloat(goodData2[1]));

                            });

                            $.each(goodCategories, function(index, value) {
                                var prot = value;
                                options.xAxis.categories.push(value);

                            });

                            options.series.push(series);
                            //console.log(options);
                        } catch (err) {
                            //console.log("ERROR ..." + err.description + '  message:'
                            //+ err.message);

                        }*/

                    }

                    function loadDataAndCreateChart() {
                        loadData();
                        //console.log(options);
                        chart = new Highcharts.Chart(options);
                    }

                    loadDataAndCreateChart();

                });
    </script>

</h:head>
<h:body>

    <div id="container"></div>

    <h:inputHidden value="#{selectCategory.jsonResults }" id="hiddenData" />
    <h:inputHidden value="#{selectCategory.jsonResultsCategories }" id="hiddenCategories" />
    <h:inputHidden value="#{selectCategory.menuItem}" id="clickedMenu" />
    <h:inputHidden value="#{selectCategory.maxDays}" id="hiddenMaxDays" />

</h:body>
</html>


You're using Facelets, which is a XML based view technology. JavaScript operators like >, &, etc are special characters in XML and are illegal when used for other purposes inside a XML file. Facelets is taking care of them this way.

I strongly recommend to put all that JS code in its own .js file and reference it by a <script src> or a <h:outputScript>.

E.g.

<script src="#{request.contextPath}/js/global.js"></script>

or

<h:outputScript name="js/global.js" />

Additional advantage is that you can finetune browser caching this way and end up in a more efficiently served website.


Unrelated to the concrete problem: please note how I referenced the libraries. You should prefer domain-relative paths over URI-relative paths. Using ../ is a maintenance pain. If you move the view or change the request URI, it'll break.


&gt; is the HTML entity which must be used to output the > character in HTML. < and > are special characters used by the HTML markup and must thus be escaped to &lt; and &gt; if these characters must be displayed in the page. I suspect you're using a JSF tag which escapes its input.

I don't have much experience with JSF, but you might try to enclose the script in a CDATA section:

<script type="text/javascript">
<![CDATA[
   your JavaScript code here
]]>
</script>

But the best solution is not to have JavaScript in HTML at all, and load it from an external file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜