Referencing Custom Javascript and CSS Files
Note: This page applies primarily to the classic form designer.
In addition to writing JavaScript and CSS on the CSS and JavaScript page, you can also include references to other JavaScript and CSS files. Including external JavaScript and CSS files can make it easier to maintain your code.
To add JavaScript or CSS files to your Forms installation directory
- On the computer hosting the Forms Server, browse to C:\Program Files\Laserfiche\Laserfiche Forms\Forms\js. Put any JavaScript or CSS files you want to use in this directory.
To reference a JavaScript file on the Forms Server from the CSS and JavaScript page
- Enter the following code in the JavaScript section of the CSS and JavaScript page:
- Replace FormsServer with the name of your Forms Server. Replace scriptname.js with the JavaScript file's name.
- Click Save.
var script = document.createElement('script');
script.src = "http://FormsServer/Forms/js/scriptname.js";
document.getElementsByTagName('head')[0].appendChild(script);
Optional: You can also use jQuery's getScript() function. See the jQuery documentation for more information.
$.getScript('http://FormsServer/Forms/js/scriptname.js');
Important: Do not use "localhost" as the value for FormsServer. Use the server name or IP address.
Tip: Be sure to put this code inside an appropriate function so it knows when to run. For example, if the code should run when the document loads, you'd include this inside $(document).ready(function() { code goes here...}.
To reference an externally hosted JavaScript file
- Enter the following code in the JavaScript section of the CSS and JavaScript page:
- Replace ScriptURL/scriptname.js with the URL of the JavaScript file.
- Replace code goes here with your code that references the external JavaScript file.
Note: Placing your code in this function ensures that it will wait for the external script to load before running.
- Click Save.
$(document).ready(function () {
$.getScript('ScriptURL/scriptname.js', function() {
//code goes here
});
});
To reference a CSS file
- Enter the following code in the JavaScript section of the CSS and JavaScript page:
- If the CSS file is on the Forms Server, replace FormsServer with the Forms Server URL. Replace CSSFileName.css with the name of the CSS file.
- If the CSS file is hosted on a website, replace FormsServer/Forms/js/CSSFileName.css with the URL of the CSS file.
- Click Save.
$(document).ready(function () {
$('head').append('<link rel="stylesheet" href="FormsServer/Forms/js/CSSFileName.css" type="text/css" />');
});