A simple example using just numeric parameters

This web page contains JavaScript code that mimics the behaviour of a web server (or more specifically, a web template engine) that generates a web page using the web template below:

<h4>Demo of ${c}</h4> <p>This is a simple demo showing the use of string interpolation to subsitute strings inside a JavaScript template string. The template string here is <tt>str</tt>.</p> <p>We can also insert numbers instead of strings using ${c}, for example: ${a} plus ${b} = ${a + b}.</p> Normally a web server would obtain concrete values for these parameters from its back-end database, from other systems and services (e.g. the local file system or web services accessed via the internet), or from user input supplied in preceding HTTP requests. Here the values are simply hard-coded in the JavaScript or supplied in Section 2 below.

Supplying the parameters

Enter the value for ${a}  
Enter the value for ${b}  
The value of the parameter ${c} is hard-coded to the string "string interpolation" in the JavaScript code.

Generating the web page

Hitting the buttons below executes a JavaScript function generateWebPage to generate (part of) a HTML web page. The two buttons use different techniques to show the generated HTML in your browser:

  1. The top button uses an using an assignment to the DOM API property .innerHTML to inserts the HTML at the bottom of the current page, in Section 4 below. Any scripts between <script> tags in the HTML will not be executed, but it is still possible to get scripts to execute by inserting scripts without the use of explicit <script> tags.
  2. The bottom button uses the DOM API method document.write() to generate a whole new webpage containing just the generated HTML. Scripts will be executed.
The different ways that .innerHTML and document.write() should handle scripts are specified here in the HTML spec).

Because this web template only involves numeric parameters, it my not be possible to inject scripts. But given the weird behaviour that JavaScript allows, I would not be surprised if you can still inject scripts.


Below the generated webpage will appear


Here the generated webpage will appear