JavaScript 脚本标签中的绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3509113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:57:50  来源:igfitidea点击:

Absolute path in JavaScript script tag

javascriptasp.netscript-tag

提问by user384080

Is there an absolute path while declaring the tag?

声明标签时是否有绝对路径?

this will resolve if I have a aspx page in a folder (one level) script src="../Scripts/jquery-1.4.1.js" type="text/javascript">

如果我在文件夹(一级)脚本中有一个 aspx 页面,这将解决 src="../Scripts/jquery-1.4.1.js" type="text/javascript">

this will resolve if I have a aspx page in a folder (two level) script src="../../Scripts/jquery-1.4.1.js" type="text/javascript">

如果我在文件夹(两级)脚本中有一个 aspx 页面,这将解决 src="../../Scripts/jquery-1.4.1.js" type="text/javascript">

this will resolve if I have a aspx page in the main root script src="Scripts/jquery-1.4.1.js" type="text/javascript">

如果我在主根脚本中有一个 aspx 页面,这将解决 src="Scripts/jquery-1.4.1.js" type="text/javascript">

Do i really need to create different version for each relative path?

我真的需要为每个相对路径创建不同的版本吗?

回答by Daniel Vassallo

You may want to use a relative path from the domain root instead:

您可能希望使用域根目录中的相对路径:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript">

回答by Chris Fulstow

For ASP.NET MVCuse Url.Content("~/Scripts/jquery-1.4.1.js")in your view. The tilde makes your path relative to the application root, which could be a sub-folder if you're running as an IIS virtual application.

对于ASP.NET MVC,在您的视图中使用Url.Content("~/Scripts/jquery-1.4.1.js")。波浪号使您的路径相对于应用程序根目录,如果您作为 IIS 虚拟应用程序运行,它可能是一个子文件夹。

If it's WebForms, try Page.ResolveUrl()or VirtualPathUtility.ToAbsolute()in your page.

如果是WebForms,请在您的页面中尝试Page.ResolveUrl()VirtualPathUtility.ToAbsolute()

(As an aside, you might also want to consider loading jQuery from a CDN)

顺便说一句,您可能还想考虑从 CDN 加载 jQuery

回答by Goran Obradovic

When referencing scripts and css files in webforms applications, use

在 webforms 应用程序中引用脚本和 css 文件时,使用

"<%=ResolveUrl("~/path/file.ext") %>"

"<%=ResolveUrl("~/path/file.ext") %>"

This is similar to "@Url.Content("~/path/file.ext")"in MVC and will replace ~ (application root) with application base path regardless of whether is it root application on server or in some virtual directory. If you use absolute path (/path.file.ext) it may work for you when your application is in root of web site, but when you move it into virtual directory it may stop resolving resources.

这类似于"@Url.Content("~/path/file.ext")"在 MVC 中,无论它是服务器上的根应用程序还是某个虚拟目录中的根应用程序,都将用应用程序基本路径替换 ~(应用程序根目录)。如果您使用绝对路径 (/path.file.ext),当您的应用程序位于网站的根目录时,它可能对您有用,但是当您将其移动到虚拟目录时,它可能会停止解析资源。

回答by Haim Evgi

if you need jquery use can use always one absolute pathto google cdn

如果您需要 jquery 使用可以始终使用一个绝对路径到 google cdn

http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js

a good topic : what is the different form relative vs absolute paths read in :

一个很好的话题:读入的相对路径与绝对路径的不同形式是什么:

Absolute vs relative URLs

绝对与相对 URL

(Coincidence : me and @Daniel Vassallo Participants in this post)

(巧合:我和@Daniel Vassallo 在这篇文章中的参与者)

回答by David Hazel

Code inserts such as "<%=ResolveUrl("~/path/file.ext") %>"do not seem to be an option if you are using Themes. If you use them, you get an exception.

"<%=ResolveUrl("~/path/file.ext") %>"如果您使用的是主题,则诸如此类的代码插入似乎不是一种选择。如果你使用它们,你会得到一个例外。

回答by KoolKabin

I prefer using <base>tag and giving refrence as per that base tag

我更喜欢使用<base>标签并根据该基本标签提供参考

some thing like: http://www.w3schools.com/tags/tag_base.asp

类似的东西:http: //www.w3schools.com/tags/tag_base.asp

回答by John Li

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript">

This one does not work at all in web form. "/" does not represent website root dir.

这个在网络表单中根本不起作用。“/”不代表网站根目录。