简体   繁体   English

动态引用javascript文件

[英]dynamic reference to javascript file

I have a master page with a reference to javascript file that is js folder. 我有一个参考js文件夹的javascript文件的母版页。 Both File and masterpage are in the root directory. File和masterpage都在根目录中。

Many of my forms are in different folders Like 我的许多表格都在不同的文件夹中,例如

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

I kept the reference like this 我保留了这样的参考

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

I also tried src="<%= Page.ResolveUrl("~/js/Check.js") %>" and src="~/js/Check.js" . 我还尝试了src="<%= Page.ResolveUrl("~/js/Check.js") %>"src="~/js/Check.js" While using Page.ResolveUrl and Server.ResolveUrl page shows error. 使用Page.ResolveUrlServer.ResolveUrl页面显示错误。

What i need to do so that any form in any directory get the reference of the file. 我需要这样做,以便任何目录中的任何形式都可以获取文件的引用。 I dont want to do ../ and ../../ in every form. 我不想以任何形式执行../../../

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

您可以使用“ /”->网站根目录

If you're running your Visual Studio's internal server for debugging then firstly remove the virtual folder it insists on using. 如果您正在运行Visual Studio的内部服务器进行调试,请首先删除它坚持使用的虚拟文件夹。

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 / 为此,请在VS解决方案资源管理器中选择站点的根目录,然后右键单击并选择“属性窗口”-在属性中,将“虚拟路径”从“ AppName”更改为/

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. 该虚拟路径对各种路径都造成了破坏。.我发现,如果我不对VS进行此更改,则当我将所有路径都正常工作时,如果将站点放置在不可用的实时服务器上,它们将无法工作不使用虚拟站点。

Then set your JS reference to <script type="text/javascript" src="/js/Check.js"></script> - using the root / in your src. 然后将JS引用设置为<script type="text/javascript" src="/js/Check.js"></script> -使用src中的root /

UPDATE UPDATE

If you really want to keep the virtual folder then you can try this - using ResolveClientUrl() : 如果您确实要保留虚拟文件夹,则可以尝试使用ResolveClientUrl()

First, add Runat="Server" to your <head> 首先,将Runat="Server"添加到您的<head>

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

<script type="text/javascript" src='<%=ResolveClientUrl("~/js/check.js") %>' />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM