javascript 如何将波浪号的相对路径渲染成jquery/javascript中相对路径的../../?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7397939/
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-10-25 23:59:03  来源:igfitidea点击:

how to render the relative path with tilde into ../../ of relative path in jquery/javascript?

c#javascriptjqueryasp.nethtml

提问by DEN

Well, i understand my title is a bit confusing. I will state it clearly below with the example.

好吧,我知道我的标题有点令人困惑。我将在下面通过示例清楚地说明。

<asp:ImageButton ID="imgButton" NavigateUrl="~/Presentation/Resources/Images/addFile.png" runat="server" />

In html, the control above will be rendered as

在 html 中,上面的控件将呈现为

<input type="image" name="imgButton" id="imgButton" src="../../Resources/Images/addFile.png" style="border-width:0px;">

I notice that,it will convert the src from "~" to "../../" .It auto arrange it will the file level.

我注意到,它会将 src 从“~”转换为“../../”。它会自动排列文件级别。

so in javascript, i want to set it the control with this url :

所以在 javascript 中,我想用这个 url 设置它的控件:

~/Presentation/Resources/Images/PDF.png

~/Presentation/Resources/Images/PDF.png

unfortunately, in html it will be rendered as

不幸的是,在 html 中它将被呈现为

<input type="image" name="imgButton" id="imgButton" src="~/Presentation/Resources/Images/addFile.png" style="border-width:0px;">

My question is, What should i write if i wanna get the "../../" relative path with "~" ?I have tried this,but i cant get it.

我的问题是,如果我想获得带有“~”的“../../”相对路径,我应该写什么?我试过这个,但我不能得到它。

<script type="javascript">
document.getElementById("<%= imgButton.ClientID %>").src = 
"~/Presentation/Resources/Images/PDF.png";
</script>

回答by Tim Schmelter

Try this: http://weblogs.asp.net/joelvarty/archive/2009/07/17/resolveurl-in-javascript.aspx

试试这个:http: //weblogs.asp.net/joelvarty/archive/2009/07/17/resolveurl-in-javascript.aspx

In the master page for the site, put this:

在站点的母版页中,输入以下内容:

<script type="text/javascript">
        var baseUrl = "<%= ResolveUrl("~/") %>";
</script>

Then, in your javascript file, put this function:

然后,在您的 javascript 文件中,放置此函数:

function ResolveUrl(url) {
    if (url.indexOf("~/") == 0) {
        url = baseUrl + url.substring(2);
    }
    return url;
}

You could have put the function right in the master page, but then you wouldn't get intelli-sense on it for the rest of your code. Now you can call ResolveUrl with ~/ right from javascript.

您可以将函数直接放在母版页中,但是这样您就不会在其余代码中获得智能感知。现在,您可以直接从 javascript 中使用 ~/ 调用 ResolveUrl。

Why do you need this on clientside? Use servercontrols(runat=server) and you can use tilde to resolve URL on server.

为什么在客户端需要这个?使用 servercontrols( runat=server),您可以使用波浪号来解析服务器上的 URL。

回答by tsimbalar

Actually, URLs with tilde get converted to absolute URL thanks to the methods : ResolveURLand ResolveClientURL

实际上,由于以下方法,带有波浪号的 URL 被转换为绝对 URL: ResolveURLResolveClientURL

Therefore you should be able to do this :

因此你应该能够做到这一点:

<input type="image" name="imgButton" id="imgButton" src="<%=this.ResolveClientUrl("~/Resources/Images/addFile.png")%>" style="border-width:0px;">

(this is actually done automatically for you in Web Controls like HyperLink and such)

(这实际上是在诸如 HyperLink 之类的 Web 控件中为您自动完成的)

The big difference with those two methods happens when you use User-controls. In a case, the URL refers to the URL relative to the folder where the user-control is, in the other case, that would be the page containing the user-control.

当您使用用户控件时,这两种方法的巨大差异就会发生。在一种情况下,URL 是指相对于用户控件所在文件夹的 URL,在另一种情况下,该文件夹将是包含用户控件的页面。

See also this question : Control.ResolveUrl versus Control.ResolveClientUrl versus VirtualPathUtility.ToAbsolute

另请参阅此问题:Control.ResolveUrl 与 Control.ResolveClientUrl 与 VirtualPathUtility.ToAbsolute

回答by Jonas Stensved

Have a look at the VirtualPathUtility Classand this information from msdn about ASP.NET project paths.

查看VirtualPathUtility Class和 msdn 中有关ASP.NET 项目路径的信息

The VirtualPathUtility.ToAppRelative method is probably what you're looking for.

VirtualPathUtility.ToAppRelative 方法可能就是您正在寻找的方法。

VirtualPathUtility.ToAppRelative

If the virtual path for the application is "myapp" and the virtual path "/myApp/sub/default.asp" is passed into the ToAppRelative method, the resulting application-relative path is "~/sub/default.aspx".

VirtualPathUtility.ToAppRelative

如果应用程序的虚拟路径是“myapp”并且虚拟路径“/myApp/sub/default.asp”被传递到 ToAppRelative 方法中,那么生成的应用程序相对路径是“~/sub/default.aspx”。

It explains and gives examples on how to convert between different path formats.

它解释并给出了有关如何在不同路径格式之间转换的示例。

I also think you should to output the correct path at server level instead of reverse engineering in javascript in the browser. It may cause issues if you rearrange your project and asp changes it.

我还认为您应该在服务器级别输出正确的路径,而不是在浏览器中的 javascript 中进行逆向工程。如果您重新安排您的项目并且 asp 更改它,它可能会导致问题。

回答by Mark Redman

WebControls translate the tilde into the correct path when run on the server before rendering the html, you will need to use the full path or relative path in jQuery if you're changing the src on the fly.

在渲染 html 之前在服务器上运行时,WebControls 将波浪号转换为正确的路径,如果您正在动态更改 src,则需要在 jQuery 中使用完整路径或相对路径。

You might want to have a property on the page with the root path eg: ApplicationRootURL and do something like this:

您可能希望在具有根路径的页面上拥有一个属性,例如:ApplicationRootURL 并执行如下操作:

<script type="javascript">
document.getElementById("<%= imgButton.ClientID %>").src = 
"<%=ApplicationRootURL%>/Presentation/Resources/Images/PDF.png";
</script>