views:

579

answers:

5

any smart way of doing a "root" based path referencing in javascript just the way we have ~/ in asp.net ?

+4  A: 

~/ is the application root and not a literal root, it interpets ~/ to mean <YourAppVirtualDir>/

To do a literal root in JavaScript it's just /, i.e "/root.html". There's no way of getting an application level path like that in JavaScript.

You could hack it in the ASPX file and output it in a tag but I would consider the security implications of that.

Lloyd
While your definition of ~/ is correct, it's a little defeatist to say there is no way of getting this through javascript, it just takes a little imagination is all.
BenAlabaster
Well one is entirely server side and the other isn't though as I said you `can` bring it through to the client side but I personally wouldn't :)
Lloyd
What security implications could there be in exposing your application root? An attacker already knows that the application root is one of the nesting levels of the page's URL, don't they?
AaronSieb
Yes, I was thinking along the lines of ~/ ending up expanded but that wouldn't happen.
Lloyd
+12  A: 

Have your page generate a tag with something like:

<link rel="home" id="ApplicationRoot" href="http://www.example.com/appRoot/" />

Then, have a function in JavaScript that extracts the value such as:

function getHome(){
    return document.getElementById("ApplicationRoot").href;
}
MiffTheFox
modified the example *slightly* to make it a little less ambiguous
BenAlabaster
Note: To generate the tag, code like this should help: string urlPath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
Brian
+1 for creativity. Neat idea.
Chris Lively
If you're going to use server-side code to generate the value, why do you need to put it in a <link> element and retrieve it with getElementById? Just do <script>var home = '<%= HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath %>';</script>
Grant Wagner
Putting it in a <link> tag allows you to keep JS separated from your markup. Of course, web forms throws quite a bit of JS into your markup anyway, so the variable might be easier in that environment.
AaronSieb
+5  A: 

I usually create a variable at the top of the js file and assign it the root path. Then I use that variable when referencing a file.

var rootPath = "/";
image.src = rootPath + "images/something.png";
RedWolves
+1 this is my approach, too
Gabe Moothart
+1 this is how i always do it.
Amr ElGarhy
+8  A: 

Use base tag:

<head>
   <base href="http://www.example.com/myapp/" />
</head>

...

from now any link use on this page, no matter in javascript or html, will be relative to the base tag, which is "http://www.example.com/myapp/".

Kamarey
really useful. did not know about this tag.
Vikram
Thanks for this answer. This helped me immensely.
Brian Hasden
+3  A: 

You could also use the asp.net feature VirtualPathUtility:

<script>
var basePath = '<%=VirtualPathUtility.ToAbsolutePath("~/")%>';
</script>

Notice: I don't encode the path to a JSON-string (escape quotes, control characters etc). I don't think this is a big deal (quotes for example aren't allowed unescaped in an URL), but one never knows...

doekman