Using JavaScript

Contents

JavaScript is an essential tool for making your web pages interactive. In most cases, it is the primary language for client-side scripting — that is, all the site-specific code that runs in the browser.

There are two ways you can use JavaScript:

  1. Put the JavaScript in a separate “.js” file and include it by reference.
  2. Include JS code, surrounded by a <script> tag, directly in your page template or page body.

To create a new JS file, choose JS Files from the Structure menu, and then click Add JavaScript File. Enter the name for the JS file (including the .js extension) and your JS code and click Save.

You can also upload JS files as assets, by choosing Images and Files from the Content menu, clicking New File, and selecting the file on your computer (just as you would to upload an image). If you do so, however, you will not be able to edit the JS code from within the browser. Uploading the JS as a file is usually the best approach for library code that you don't plan to modify, whereas using the JS Files admin interface is best for files that you want to be able to edit easily.

To include the JS from the file in a template, a snippet, or pod contents, use the following code:

<script src="/my_javascript_file.js" type="text/javascript"></script>

Note that all files referenced by Webvanta sites should begin with a leading slash.

If you are using the standard Webvanta starter site, you'll find that the templates already include the jQuery JavaScript library. We highly recommend using jQuery to make your JavaScript programming easier and to avoid browser compatibility issues.

If you are inserting JS code directly into a template, a snippet, or pod contents, just wrap it in script tags as in any HTML file:

<script type="text/javascript">
   Your JS code here
</script>

Add Your Comments

(not published)