最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set relative path to a "a href"? - Stack Overflow

programmeradmin1浏览0评论

For example, I provide an relative to an social sharing plugin

../../test.html

as a link

It show as

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

Is there any way to convert the path to absolute if it requires an "http://" before the path ? Thanks

Edit:

The place I put the path

$('#sina').attr('href', "javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='.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'));");

I dont know how the path works but it works if I provide with absolute path

#sina is a <a href></a> element while imgStr is the link provide

I check the source code after running the website , if it is relative path , it show as "http://../../test.html"

For example, I provide an relative to an social sharing plugin

../../test.html

as a link

It show as

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

Is there any way to convert the path to absolute if it requires an "http://" before the path ? Thanks

Edit:

The place I put the path

$('#sina').attr('href', "javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina..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'));");

I dont know how the path works but it works if I provide with absolute path

#sina is a <a href></a> element while imgStr is the link provide

I check the source code after running the website , if it is relative path , it show as "http://../../test.html"

Share Improve this question edited Aug 22, 2013 at 8:24 user1871516 asked Aug 22, 2013 at 8:16 user1871516user1871516 1,0096 gold badges15 silver badges26 bronze badges 5
  • 2 I'm afraid your question is very unclear. You provide the relative path where? What does that markup/code look like? "It show as..." Where? What are you looking at? – T.J. Crowder Commented Aug 22, 2013 at 8:18
  • What is the server side platform? – gdoron Commented Aug 22, 2013 at 8:19
  • Thanks for the reply, sorry for unclear , let me edit it – user1871516 Commented Aug 22, 2013 at 8:19
  • This sounds like a problem with the plugin, it's adding the http:// prefix, so your relative path bees absolute. – Barmar Commented Aug 22, 2013 at 8:20
  • @Barmar you are correct , Is the workaround replace ../ to location.href or something similiar? – user1871516 Commented Aug 22, 2013 at 8:28
Add a ment  | 

1 Answer 1

Reset to default 3

Try

location.hostname+location.pathname+your_relative_path

This will give you a bination of the path to your current page (minus the actual file name if there was one in the url) with your relative address in your_relative_path.

So, something like http://www.mypage./contents/level1/en-us/page2/../../test.html would be the result in the end.

发布评论

评论列表(0)

  1. 暂无评论