In Exchange Capture, you can create, edit, and delete blocks of JavaScript, or other code in the form of code snippets.
You create code snippets and save them with a unique name. Then, you can reference them in page groups, inserting one or more code snippets in the pages that are associated with that page group.
Best practices code snippets
You can employ code snippets best practices for fast deployment. Keep in mind the following caveats and best practices.
Raw HTML is not supported in code snippets. However, you can create HTML document objects such as iframes or forms using JavaScript in code snippets.
Using document.write in code snippets
You are prevented from using document.write in code snippets that are used in default tags (tags that are inserted in the body section of your web page), because default tags run asynchronously. It is impossible to predict when the write action happens. For example, if the code snippet runs after the body rendering finishes and after the DOM is loaded, the page content will be entirely rewritten.
You can use document.write in code snippets that are used in head tags (tags that are inserted in the head section of your web page). The content that is written is always inserted at the start of the body section.
Local and global variables and functions in code snippets
Any variables declared within a code snippet with the keyword var is local to that code snippet.
Any functions explicitly declared within a code snippet can be referenced only by that code snippet. Only
globally created functions are accessible across code snippets.
To create a globally accessible function, you can use the following code within your code snippet:
if (typeof(window["myFunction"]) !== "function") {window["myFunction"] = function(param1, param2, ..., paramN)
{alert("This is a global function"); }}
Keep in mind that code snippets can be used in multiple page groups, and that the order in which the page groups execute determine whether code snippets are able to access global functions and variables declared in other code snippets. It is recommended that before you create a new code snippet for use in page groups, you check for the existence globally of any functions or variables that you require, and ensure that the code snippets are created and initialized based on the order in which the page groups execute.
Creating code snippets
Use this procedure to create a code snippet. Raw HTML is not supported in code snippets. However, you can create HTML document objects such as iframes or forms using JavaScript in code snippets.
-
From the left panel, select Code snippets.
-
Click Create snippet.
-
Enter a unique name for the code snippet.
- Optionally, add a description for your code snippet.
-
Enter the Javascript code in the Code snippet details box. Your code will be validated before you save.
-
Click Add code snippet.
Editing code snippets
You can edit code snippets at any time.
-
Click Code snippets.
-
Locate the code snippet that you want to edit.
-
From the three-dot menu, select Edit and edit any of the information.
-
Click Update code snippet.
Deleting code snippets
Use this procedure to delete selected code snippets. To delete any code snippets that are currently deployed in either the test or production environments, you must first remove the code snippets from deployment.
-
Click Code Snippets.
-
Select the code snippet that you want to delete.
-
Click the three-dot menu and select Delete.
-
Click Yes to confirm that you want to delete the code snippets.
If any of the code snippets that you select for deletion are currently deployed in the test or production environments, you see a warning.