Relative Path, but for Port?

2019-01-27 12:26发布

We are all familiar with relative paths: A relative path to ./images/hello.jpg from http://www.domain.com/hey links to http://www.domain.com/hey/images/hello.jpg.

Problem: How do you state a relative path to http://www.domain.com:1234 when you are at http://www.domain.com/hey?

4条回答
叼着烟拽天下
2楼-- · 2019-01-27 12:49

Simply you can write in href attribute:

/:port/[path/]file.ext
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-27 12:53

This can be achieved using JavaScript by setting the window.location.port property.

<a href="#" onclick="javascript:window.location.port=8080">go</a>
查看更多
时光不老,我们不散
4楼-- · 2019-01-27 12:54

You cannot change any part of the authority (i.e. the host:port part) in relative URLs. See the algorithm described in section 5.2.2 of RFC 3986 to see how relative URLs are interpreted. Important thing to notice is that authority is simply copied from base URL or from the URL being resolved and authority's structure is never interpreted. This implies that you cannot change any of its parts, including the port part.

Here's the algorithm in pseudo-code copied from the RFC:

  -- The URI reference is parsed into the five URI components
  --
  (R.scheme, R.authority, R.path, R.query, R.fragment) = parse(R);

  -- A non-strict parser may ignore a scheme in the reference
  -- if it is identical to the base URI's scheme.
  --
  if ((not strict) and (R.scheme == Base.scheme)) then
     undefine(R.scheme);
  endif;

  if defined(R.scheme) then
     T.scheme    = R.scheme;
     T.authority = R.authority;
     T.path      = remove_dot_segments(R.path);
     T.query     = R.query;
  else
     if defined(R.authority) then
        T.authority = R.authority;
        T.path      = remove_dot_segments(R.path);
        T.query     = R.query;
     else
        if (R.path == "") then
           T.path = Base.path;
           if defined(R.query) then
              T.query = R.query;
           else
              T.query = Base.query;
           endif;
        else
           if (R.path starts-with "/") then
              T.path = remove_dot_segments(R.path);
           else
              T.path = merge(Base.path, R.path);
              T.path = remove_dot_segments(T.path);
           endif;
           T.query = R.query;
        endif;
        T.authority = Base.authority;
     endif;
     T.scheme = Base.scheme;
  endif;

  T.fragment = R.fragment;
查看更多
别忘想泡老子
5楼-- · 2019-01-27 12:54

Simple answer: not possible. You need to use an absolute path if the host changes.

查看更多
登录 后发表回答