-1

I want to define two or more var in my javascript code,but when I define the second one, the first one becomes invalid.I have searched this problem, but I don't why. Can anybody tell me how to deal with this? Here is my javascript code(It's used to convert JSON to HTML table):

<script type="text/javascript">
         var v_first = {{ pass_kegg|safe }}
         var v_seconde = {{ pass_tmp|safe }}

         function buildHtmlTable(myList) {
            //Builds the HTML Table out of myList json data from Ivy restful service.
         }

         function addAllColumnHeaders(myList)
         {
             //Adds a header row to the table and returns the set of columns.Need to do union of keys from all records as some records may not containall records
         }
    </script>

When I define only one variable,all things works fine. But when I fine the second variablev_second,the first variable v_first becomes invalid.

Phil
  • 157,677
  • 23
  • 242
  • 245
Coding_Rabbit
  • 1,287
  • 3
  • 22
  • 44
  • 1
    Javascript statements end in semicolons. – Luke Joshua Park Apr 22 '16 at 01:25
  • 1
    In the `var v_first = {{ pass_kegg|safe }}` part, what are the double braces for? Are they being replaced at the server side? If not, remove them, to make it `var v_first = pass_kegg|safe` and `var v_seconde = pass_tmp|safe` and see if that does what you want. – Joseph Sikorski Apr 22 '16 at 01:27
  • @LukePark Make it an answer and I'll vote for it – nhouser9 Apr 22 '16 at 01:28
  • Thank you for you guys' answer, the `pass_kegg` is a variable passed from Flask App,so it will be replaced at the server side. – Coding_Rabbit Apr 22 '16 at 01:30
  • What do you mean by *"... becomes invalid"*? What does the generated script look like (you can see it by viewing the page source)? – Phil Apr 22 '16 at 01:31

2 Answers2

1

Seems you should be using the tojson() filter for dynamic JavaScript generation, ie

var v_first = {{ pass_kegg|tojson|safe }},
    v_seconde = {{ pass_tmp|tojson|safe }};

It's good practice to terminate expressions with a semi-colon but it's definitely not required. See What are the rules for JavaScript's automatic semicolon insertion (ASI)?

Community
  • 1
  • 1
Phil
  • 157,677
  • 23
  • 242
  • 245
-1

Try this

     var v_first = { pass_kegg|safe };
     var v_seconde = {pass_tmp|safe };

instead of this

     var v_first = {{ pass_kegg|safe }}
     var v_seconde = {{ pass_tmp|safe }}

this may work not 100% on it

MNM
  • 2,673
  • 6
  • 38
  • 73