views:

56

answers:

3

Hi Guys

I have some pages that reference javascript files.

The application exists locally in a Virtual Directory, i.e. http://localhost/MyVirtualDirectory/MyPage.aspx

so locally I reference the files as follows:

<script src="/MyVirtualDirectory/Scripts/MyScript.js" type="text/javascript"></script>

The production setup is different though. The application exists as its own web site in production, so I don't need to include the reference to the virtual directory. The problem with this is that I need to modify every file that contains a javascript reference so it looks like the following:

<script src="../Scripts/MyScript.js" type="text/javascript"></script>

I've tried referencing the files this way in my local setup but it doesn't work.

Am I going about this completely wrong? Can somebody tell me what I need to do?

Thanks

+7  A: 

Use ResolveUrl("~/")

<script src="<%=ResolveUrl("~/scripts/myscript.js") %>" type="text/javascript"></script>

~/ will get to you the root of your application, virtual or otherwise

hunter
+1 Another good option.
technophile
@DaveDev it's probably a good idea to use ~/ for all of your resources as well; javascript, stylesheets, images, etc.... it will save you a lot of headaches
hunter
His second example, though, has `Scripts` as a peer level to his application directory, not a subdirectory of it, doesn't it? It starts with `../` after all... Whereas in his first example, `Scripts` clearly is a subdirectory of the app root and so this would work (although it seems unnecessary; it can just be a relative path without server-side parsing required at all). Of course, he did say the second example didn't work. :-)
T.J. Crowder
@TJ I think he's assuming that he has to navigate back "../" because he no longer has the virtual directory in the production environment
hunter
If `Scripts` is always a subdirectory of the application, this is over-complicated. Just use a relative path without any server-side parsing.
T.J. Crowder
A: 

You can use the HttpRuntime.AppDomainAppVirtualPath property to get the virtual path (if any) for your app and use that to fix up the javascript paths (using <%= ... %> in the <script> tags etc.)

You can further add a global javascript variable in your master page that exposes that value as well, so that any scripts that need to know the actual app root can access it that way.

technophile
+1  A: 

Previous answers seem to assume that the Scripts directory always exists as a subdirectory of your application root. If that assumption is correct, and if the page is also at the root, then both of your earlier tags can be simply:

<script src="Scripts/MyScript.js" type="text/javascript"></script>

But my read of your second example is that Scripts isn't always a subdirectory of your application root (because the ../ at the beginning moves up a level, so Scripts would be a peer of your application root). That said, you did say the second example didn't work. :-) But if that's really the case, I'd strongly recommend adjusting one environment or the other so that the relative paths agree, and then always using relative paths as above.

The only reason for using ResolveUrl as far as I know would be if the pages in the application are in a folder structure and the tag may appear in a page at the root or in a page in a "subdirectory". If so, you can use ResolveUrl in both cases so you have an anchor point. I never author things that way, I always ensure I know where the page will be in the hierarchy (if there needs to be a hierarchy) and use an appropriate relative path for the current document.

T.J. Crowder
<script src="Scripts/MyScript.js" type="text/javascript"></script> - won't that ONLY work if the page is at the root of the site?
hunter
@Hunter: No, it's a *relative* path from the current document's location. For it to be from the root of the site, it would start with a slash (`/`).
T.J. Crowder
@TJ but if I'm at "/myfolder/index.html" then "scripts/myscripts.js" will resolve to "/myfolder/scripts/myscripts.js", correct?
hunter
@Hunter: Yes, it's a relative path.
T.J. Crowder
@TJ exactly. which will not be the correct path to the scripts folder, which is why doing the ~/ will always get the correct path regardless of depth
hunter
@Hunter: We have different understanding of his structure, I think; I've laid out my understanding of it fairly clearly above. I think you're assuming the page will be in a subdirectory somewhere, which he hasn't said it will be. (Doesn't mean you're not right, just that he hasn't *said* that. :-) )
T.J. Crowder
DaveDev
@DaveDev - thanks! I think this solution would only work if you had a flat (no pages in subfolders) project, though, which might be the case
hunter
@Hunter: No, it works regardless, provided you allow for where the page is in the hierarchy correctly. If `Scripts` maps to `http://example.com/Scripts`, for instance, and you have a page at `http://example.com/a/page.aspx`, then the relative path is `../Scripts/MyScript.js`. If the page is at `http://example.com/page.aspx`, then the path is `Scripts/MyScript.js`. If the page is `http://example.com/a/b/c/page.aspx`, the relative path is `../../../Scripts/MyScript.js`. Etc. (I've seen actual examples of this where someone backed up eight levels. I don't recommend that. :) )
T.J. Crowder
@TJ Right, that's what I meant. I guess I'm so used to using MasterPages that I usually set it once there with the "~/" and forget it about it. My solution was geared towards setting that in the MasterPage.
hunter