{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":615,"path":"api-reference/scripts/c_UIScripts.md","title":"UI scripts","bundle":null,"doc_type":null,"release":"australia","body":"\n# UI scripts\n\nUI scripts provide a way to package client-side JavaScript into a reusable form, similar to how script includes store server-side JavaScript. Administrators can create UI scripts and run them from client scripts and other client-side script objects and from HTML code.\n\n**Note:** UI scripts aren't supported on mobile or Next Experience.\n\n**Parent Topic:**[Client-side scripting](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/client-side-scripting-overview.md)\n\n## Global UI scripts\n\nYou can create a UI script and designate it as global, which makes the script available on any form in the system. You cannot create a global UI script in a scoped application.\n\nYou can mark a UI script as Global to make it available on any form in the system. For example, you can create a UI script that has a function helloWorld\\(\\), and has the **Global** field checked:\n\n```\nfunction helloWorld() {\n  alert('Hi');\n}\n```\n\nAfter you create this global UI script, you can call the helloWorld\\(\\) function from any client script or UI policy you write.\n\n## Create a UI script\n\nCreate a UI script to define reusable client-side JavaScript code.\n\n### Procedure\n\n1.  To create UI scripts, navigate to **System UI** &gt; **UI Scripts** and create or edit a record \\(see table for field descriptions\\).\n\n<table id=\"table_zsc_z4z_sr\"><thead><tr><th>\n\nField\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nScript Name\n\n</td><td>\n\nName of the UI script. Ensure the name is unique on your system.\n\n</td></tr><tr><td>\n\nAPI Name\n\n</td><td>\n\nThe API name of the UI script, including the scope and script name \\(for example, x\\_custom\\_app.HelloWorld\\).\n\n</td></tr><tr><td>\n\nApplication\n\n</td><td>\n\nApplication that contains the UI script.\n\n</td></tr><tr><td>\n\nActive\n\n</td><td>\n\nIndicator of whether the UI script is active. Only active UI scripts can run.\n\n</td></tr><tr><td>\n\nGlobal\n\n</td><td>\n\nIndicator of whether the script loads on every page in the system.\n\n **Note:** Use caution when creating global UI scripts because they can impact performance. You cannot create a global UI script in a scoped application.\n\n</td></tr><tr><td>\n\nDescription\n\n</td><td>\n\nSummary of the purpose of the script.\n\n</td></tr><tr><td>\n\nScript\n\n</td><td>\n\nClient-side script to run when called from other scripts.\n\n</td></tr></tbody>\n</table>\n## Run UI scripts\n\nFollow these guidelines when running UI scripts.\n\n### Run a UI script from a form\n\nTo run a UI script on a form, [Create a formatter and add it to a form](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/t_CreateAFormatter.md). In the associated [UI macro](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_UIMacros.md), include a `g:requires` tag and specify the **name=** parameter as the name of the UI script followed by the .jsdbx extension. Add the formatter on the form view.\n\nThis code ensures that the definitions and results of the UI script are immediately available in the browser.\n\n```\n<?xml version=\"1.0\" encoding=\"utf-8\" ?>  \n<j:jelly trim=\"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">  \n    <g2:evaluate var=\"jvar_stamp\">  \n        var now_GR = new GlideRecord('sys_ui_script');  \n        gr.orderByDesc('sys_updated_on');  \n        gr.query();  \n        gr.next();  \n        gr.getValue('sys_updated_on');  \n    </g2:evaluate>  \n    <g:requires name=\"<UI SCRIPT NAME>.jsdbx\" params=\"cache=$[jvar_stamp]\" />  \n</j:jelly>\n```\n\n### Call a UI script in HTML\n\nTo run a UI script from HTML code, use the `<script>` tag and specify the `src=` argument as the API name of the UI script followed by the .jsdbx extension. For example, include the UI script named CoolClock with this code:\n\n```\n<script language=\"javascript\" src=\"CoolClock.jsdbx\" />\n```\n\n### Call a UI script from client-side code\n\nAccess UI scripts from within client-side code using the g\\_ui\\_scripts global object. For more information, see [GlideUIScripts - Client](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/GUIScriptsAPI.md).\n\n**Note:** This class does not support UI scripts with the **Global** field set to true.\n\n"}}