jms1980
jms1980

Reputation: 1075

Defining a variable in html and passing it on to a js file

I have a variable in a config.js file:

var version_number=1.0;

Now what I want to happen is to get this variable to display in this template file:

var arrival_template = " [== Start of Message]\n \
[REPORT TYPE                                         : ARRIVAL REPORT] \n \
[Version                                             : <#$version#>] \n \
SECTION                                             : Parcel #] \n \
[Parcel Name                                         : <#$parcelname$#>] \n \
[IMO Number                                          : <#$imo$#>] \n \
[Call Sign                                           : <#$call_sign$#>] 

\n \ [== End of Message]";

What fills in the values to the template.gs file is an html page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en"  >
   <head>
      <script src="js/templates/template.js"></script>
      <script type="text/JavaScript" src="js/config.js"></script>
      <script type="text/JavaScript" src="js/html_util/parcel.js"</script>
   </head>

   <body>
      <div id="parcel_info_section" sytle="border:none;font-family:arial;font-size:0.8em;">
         <hr>
         <table width=100% border=0>
            <tr>
               <td align=center><label style="font-family:arial;font-weight:bold;font-size:1.2em;color:#000080;">Parcel</label></td>
               <td width=30%>&nbsp;</td>
            </tr>
          </table>
          <table width="840px" align="center" cellspacing=0 cellpadding=0 border=2>
            <tr><td colspan="3"><span id="parcel_body"></span></td></tr>
          </table>
       </div>


   </body>
</html>

What should I put in the head or the body to allow me to pass on the variable defined in config.js into the template? This config file contains information that is constant across a series of websites which is why I want to store that information in the config file.

In the output from template file <#$version#> should 1.0 based on the fact that I defined var version=1.0 in the config.gs file.

Upvotes: 0

Views: 287

Answers (1)

Nunchy
Nunchy

Reputation: 948

First, you need to include the config properly, your script tag including parcel.js is missing a closing >:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en"  >
   <head>
      <script src="js/templates/template.js"></script>
      <script type="text/JavaScript" src="js/config.js"></script>
      <script type="text/JavaScript" src="js/html_util/parcel.js"></script>
   </head>

   <body>
      <div id="parcel_info_section" sytle="border:none;font-family:arial;font-size:0.8em;">
         <hr>
         <table width=100% border=0>
            <tr>
               <td align=center><label style="font-family:arial;font-weight:bold;font-size:1.2em;color:#000080;">Parcel</label></td>
               <td width=30%>&nbsp;</td>
            </tr>
          </table>
          <table width="840px" align="center" cellspacing=0 cellpadding=0 border=2>
            <tr><td colspan="3"><span id="parcel_body"></span></td></tr>
          </table>
       </div>


   </body>
</html>

The config.js is defining the variable, you should be able to do somthing like this in your template:

var arrival_template = " [== Start of Message]\n \
[REPORT TYPE                                         : ARRIVAL REPORT] \n \
[Version                                             : <#$" + version_number + "#>] \n \
SECTION                                             : Parcel #] \n \
[Parcel Name                                         : <#$parcelname$#>] \n \
[IMO Number                                          : <#$imo$#>] \n \
[Call Sign                                           : <#$call_sign$#>] 
\n \ [== End of Message]";

Assuming that's where you actually want to place the value - not too sure, a little vague but hope this helps.

Upvotes: 1

Related Questions