且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何呈现与波浪相对路径到../../ jQuery中/ JavaScript的相对路径?

更新时间:2023-02-09 23:13:31

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


  

在该网站的主页,把这个:


块引用>
 <脚本类型=文/ JavaScript的>
        变种的baseUrl =下;%= RESOLVEURL(〜/)%>中;
< / SCRIPT>


  

然后,在你的JavaScript文件,把这个功能:


块引用>
 函数RESOLVEURL(URL){
    如果(url.indexOf(〜/)== 0){
        URL = +的baseUrl url.substring(2);
    }
    返回URL;
}


  

您可能已经把在母版页功能吧,但你
  不会得到智能影音意义上为您的code的其余部分。轮到你了
  可以用〜/右从JavaScript调用RESOLVEURL。


块引用>

为什么需要这对客户方?使用servercontrols( RUNAT =服务器),你可以用波浪来解决服务器的URL。

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

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

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

~/Presentation/Resources/Images/PDF.png

unfortunately, in html it will be rendered as

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

Try this: 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:

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.

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