2010-11-04 68 views
0

我使用下面的函数进行虚拟化,以我的应用程序资源虚拟化在jQuery代码的链接外部JavaScript内

<%= Html.Image(Url.Content("~/_assets/images/login.png"), "Login")%> 

路径和参考,这工作很细,解决虚拟目录,其中应用程序已部署,例如

http://someserver/xyz/_assets/images/login.png 

我怎么能指着资源CSS内部时,得到相同的结果

body { background: #F4F4F4 url('/_assets/images/backgr_grad.png') repeat-x 0 0; } 

并从一个外部js文件中的JavaScript函数?

function loadCustomers() { 
    $.ajax({ 
     type: "get", 
     dataType: "html", 
     url: '/Customers/AllCustomers', 
     data: {}, 
     success: function(response) { 
     } 
    }); 
} 
+0

难道你们就不能使用CSS和JS文件中的相对路径? – sheikhomar 2010-11-04 16:57:01

+0

@sheikhomar:你的意思是相对于包含css和js的masterpage位置的路径吗? – Lorenzo 2010-11-04 17:37:38

回答

2

从CSS,你总是可以使用相对路径(在CSS它是相对于CSS文件的位置):

body { 
    background: #F4F4F4 url('../images/backgr_grad.png') repeat-x 0 0; 
} 

从JS有不同的技术,但一个由视图定义一个全局JS变量:

<script type="text/javascript"> 
    var customersUrl = '<%: Url.Action("AllCustomers") %>'; 
</script> 

,然后使用该变量在外部JavaScript文件:

function loadCustomers() { 
    $.ajax({ 
     type: 'get', 
     dataType: 'html', 
     url: customersUrl, 
     data: { }, 
     success: function(response) { 
     } 
    }); 
} 

另一种技术包括渐进增强:

<%: Html.ActionLink("Load customers", "AllCustomers", 
    null, new { id = "customersLink" }) %> 

而且在JS逐步提高此链接:

$(function() { 
    $('#customersLink').click(function() { 
     $.ajax({ 
      type: 'get', 
      dataType: "html", 
      url: this.href, 
      success: function(response) { 
      } 
     }); 

     // make sure to cancel the default action 
     return false; 
    }); 
}); 
1

使用相对路径。路径是相对到CSS文件,不是页面。