KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using 4D HTML Tags inside of JavaScript
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: February 4, 2009

The use of <!-- and --> comment markers is common among JavaScript developers as a way to keep older browsers from parsing the JavaScript contained within their <script> tags. Recent browsers have been designed to be more aware of JavaScript and no longer require you to place your JavaScript code within comment markers.

4D HTML tags also use these comment markers. When using 4D tags inside of JavaScript you must be sure not to encapsulate the JavaScript within the <!-- and --> comment markers. If, however, you still want to use comment markers you can use the XML style of comment markers instead of the HTML style and the 4D tags will still be interpreted as normal.

The following block of code will not work because the 4DVAR tag is located within the <!-- and --> comment markers of the script section:

<html>
<head>
<title>4D Rocks</title>
<script type="text/javascript">
<!--
var myVar = '<!--#4DVAR othervar-->';
-->
</script>
</head>
<body>
</body>
</html>


The following example uses XML style comment markers, this will work:
<html>
<head>
<title>4D Rocks</title>
<script type="text/javascript">
//<![CDATA[
var myVar = '<!--#4DVAR othervar-->';
//]]
</script>
</head>
<body>
</body>
</html>


The following example does not use any comment markers, this will also work:

<html>
<head>
<title>4D Rocks</title>
<script type="text/javascript">
var myVar = '<!--#4DVAR othervar-->';
</script>
</head>
<body>
</body>
</html>