0

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.

2
  • 1
    what do you mean pass on the variable? if you've included the script in the header of your page, the variable will be avaliable to other js scripts once the page has loaded. Commented Jul 29, 2016 at 0:07
  • How do I make the template.js work so it displays the value of the variable as defined in the config.js file? Commented Jul 29, 2016 at 0:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

When I did what you suggested in my template the thing did not work. I have to press a submit button and that fills the variables incloude $imo $callsign, $parcelname, etc. with values. These values are entered from a table in parcel.js $version_number is defined based on the the config.js file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.