如何设置“a href”的相对路径?

时间:2013-08-22 08:16:39

标签: javascript jquery html url relative-path

例如,我提供了一个社交共享插件的亲戚

../../test.html

作为链接

显示为

http://../../test.html

如果在路径前需要“http://”,有没有办法将路径转换为绝对路径?感谢

编辑:

我放置路径的地方

$('#sina').attr('href', "javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=',u=z||d.location,p=['&url=',e(u),'&title=',e(t||d.title),'&source=',e(r),'&sourceUrl=',e(l),'&content=',c||'gb2312','&pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','" + imgStr + "','" + shareDes + "','','utf-8'));");

我不知道路径是如何工作的,但如果我提供绝对路径

,它就可以工作

#sina<a href></a>元素,而imgStr是链接提供

运行网站后检查源代码,如果是相对路径,则显示为"http://../../test.html"

1 个答案:

答案 0 :(得分:2)

尝试

location.hostname+location.pathname+your_relative_path

这将为您提供当前页面路径的组合(如果网址中有一个,则减去实际文件名)与your_relative_path中的相对地址。

因此,http://www.mypage.com/contents/level1/en-us/page2/../../test.html之类的结果最终会成为结果。