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.