People often ask about how to add custom JavaScript to their site or custom features. For some scenarios, you can add the needed scripts directly in the layout.master file of your skin, but for custom features you may not want that script on every page just on the page where your feature exists.
Adding JavaScript to a CMS page
Using the Custom JavaScript feature you can add JavaScript to any CMS page, just like any other feature. You can specify the URL to the script or paste the actual script in the settings, with options for placing the script in the head, at the bottom, or in the content position of the feature instance. You will find it in the feature list dropdown in page edit view.
Adding JavaScript to your custom feature
When developing a custom feature, you can register your script(s) from code like this:
Page.ClientScript.RegisterClientScriptBlock(typeof(Page),
"uniqueidforyourscript", "\n<script type=\"text/javascript\" src=\""
+ Page.ResolveUrl("~/ClientScript/pathtoyourscript.js") + "\" ></script>");
For initialization scripts that you want to load at the bottom of the page you can do it like this:
string initScript = "alert('hello world');"
Page.ClientScript.RegisterStartupScript(typeof(Page),
"idforyourscript", "\n<script type=\"text/javascript\" >"
+ initScript + "</script>");
Last Modified by Shawn Grout on Jul 13, 2022