Monday, April 12, 2010

ASP.Net: Referencing root-relative paths (~/) in javascript

The “~/” specifies a root-relative path in ASP.Net. ASP.Net uses the ResolveUrl and ResolveClientUrl to determine the logical path that a path containing the “~/” points to.

If you need to use a root relative path from within a JavaScript method running within an ASP.Net page, then here is what the code looks like:

<script type="text/javascript">
    function OnSuccess() {
              window.open('<%=Page.ResolveClientUrl("~/newWindow.aspx")%>', 
                'mywindow', 
                'width=400,height=200,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
      }
</script>

And here is a useful site with information on ASP.Net Paths - http://www.west-wind.com/weblog/posts/132081.aspx

No comments: