0

I have a javacript file (script.js, for example) in the following location:

/Website/Shared/Js/script.js

I have two pages which use this javascript, but each one of them seems to require a different path and I can't figure out how to resolve both of them.

One of them is the page:

/Website/One/Two/Three/page.aspx and this requires the path:

<script src="../../../Shared/Js/script.js" type="text/javascript"></script>

The other page is:

/Website/One/Two/page.aspx and this requires the path:

<script src="../../Shared/Js/script.js" type="text/javascript"></script>

I tried to come from the root by doing

<script src="../Shared/Js/script.js" type="text/javascript"></script>

or

<script src="/Shared/Js/script.js" type="text/javascript"></script>

but none of these seem to work. The temp solution I have found is to declare the script twice which is dumb, but that is all I can think of now.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432

3 Answers3

2

have you tried ResolveClientUrl() ?

<%= ResolveClientUrl("~/Shared/Js/script.js") %> 

here is a good post on the differences.

Control.ResolveUrl versus Control.ResolveClientUrl versus VirtualPathUtility.ToAbsolute

Community
  • 1
  • 1
ScottE
  • 21,530
  • 18
  • 94
  • 131
  • Or is this directly in the script tag like this: – Xaisoft Feb 15 '11 at 15:43
  • Yes, like above. I updated my answer to link to a SO post on the differences. The west wind article is good as well. – ScottE Feb 15 '11 at 15:44
  • Ok, it worked. I had to make a slight change though, not sure if It had something to do with using Telerik controls, but this is what happened. If I just put , I got the error message The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). So I wrapped in a Telerik:RadScriptBlock and it worked. Thanks – Xaisoft Feb 15 '11 at 15:50
1

you can use ~ before so server can resolve it for you.

<script src="~/Shared/Js/script.js" type="text/javascript"></script>

Another option is to use base tag but that will effect other resources too.

Adeel
  • 19,075
  • 4
  • 46
  • 60
0

I do find using the : in front to work quite well. Example

<script type="text/javascript" 
    src="<%: ResolveUrl("~/Scripts/lib/JQuery 1.7.2/jquery.min.js") %>">
   </script>
Jason Loki Smith
  • 428
  • 3
  • 12