开发者

How to resolve a javascript path for pages that have different directory levels?

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">&l开发者_运维百科t;/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.


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


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜