0

我有一个引用 js 文件夹的 javascript 文件的母版页。文件和母版页都在根目录中。

我的许多表格都在不同的文件夹中

  Root>first>abc.aspx
  Root>second>def.aspx
  Root>second>anotherFolder>def.aspx

我保持这样的参考

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

我也试过src="<%= Page.ResolveUrl("~/js/Check.js") %>"src="~/js/Check.js"。使用时Page.ResolveUrl页面Server.ResolveUrl显示错误。

我需要做什么才能使任何目录中的任何形式都获得文件的引用。我不想做../任何../../形式的事情。

4

2 回答 2

4
<script type="text/javascript" src="/js/Check.js"></script>

你可以使用 "/" --> 网站根目录

于 2013-05-15T12:12:31.253 回答
1

If you're running your Visual Studio's internal server for debugging then firstly remove the virtual folder it insists on using.

Do this by selecting the root of your site in VS Solution explorer, then right-click and choose "Properties Window" - in the properties change "Virtual Path" from your "AppName" to /

This virtual path plays havoc with all sorts of paths..I find that if i don't make this change in VS then when I DO get all my paths working they won't work when I put the site on a live server that isn't using a virtual site.

Then set your JS reference to <script type="text/javascript" src="/js/Check.js"></script> - using the root / in your src.

UPDATE

If you really want to keep the virtual folder then you can try this - using ResolveClientUrl():

First, add Runat="Server" to your <head>

<head runat="server" id="myhead">

<script type="text/javascript" src='<%=ResolveClientUrl("~/js/check.js") %>' />
于 2013-05-15T12:23:22.697 回答