将绝对路径转换为相对路径

问题描述:

通过javascript或jquery将绝对路径转换为相对路径的最佳方式是什么?将绝对路径转换为相对路径

例如考虑如下:

"http://localhost:2011/Content/Images/Product/Large/3.jpg" 

"/Content/Images/Product/Large/3.jpg" 

使用技巧,<a>元素包含位置属性。

$("<a href='http://localhost:2011/Content/Images/Product/Large/3.jpg'>") 
    .prop("pathname"); 
+0

绝妙的诀窍!你可以参考任何解释'pathname'道具的文档吗?我们可以依赖的其他道具是什么? – abernier

+0

@abernier:[这是一个列表。](https://developer.mozilla.org/en/DOM/HTMLAnchorElement#Properties)虽然它确实说明它是HTML5。我不知道跨浏览器支持。 – pimvdb

只需使用window.location.pathname

+0

和一些字符串操作将其转换为manualy? – AnyOne

+0

@AnyOne基本上 – Neal

+0

有没有自动提供的方法由JavaScript或jQuery的? – AnyOne