Page.ResolveUrl(“~/ClientScripts/Validation.js”) – Web Application Root Operator

I have the situation where I need to load a JavaScript dynamically.  All the JS files are in the ClientScript directory (http://www.somewebsite.com/ClientScripts/Validation.js).  The ASPX page that references the client scripts could be anywhere, multiple folders (directories) down in the site or in the application root.  I would like a simple way of referencing the the application root.  The magic to this is the ~ (Tidle). The ~ resolves to the root of the current application. 
 
The following line allows me to reference the application root
this.ClientScript.RegisterClientScriptInclude("Validation.js", Page.ResolveUrl("~/ClientScripts/Validation.js"));
 
And here is the results in the HTML file.
<script src="/ClientScripts/Validation.js" type="text/javascript"></script>

Leave a Reply

Your email address will not be published. Required fields are marked *