相对路径,但对于港口?

时间:2011-11-29 20:09:55

标签: http url port relative-path

我们都熟悉相对路径:从./images/hello.jpg链接到http://www.domain.com/hey的{​​{1}}的相对路径。

问题:当您在http://www.domain.com/hey/images/hello.jpg时,如何说明http://www.domain.com:1234的相对路径?

4 个答案:

答案 0 :(得分:20)

这可以通过设置window.location.port属性使用JavaScript来实现。

<a href="#" onclick="javascript:window.location.port=8080">go</a>

答案 1 :(得分:19)

您无法在相对网址中更改权限的任何部分(即 host:port 部分)。 请参阅section 5.2.2RFC 3986中描述的算法,了解如何解释相对网址。需要注意的重要一点是,只需从基本URL或正在解析的URL复制权限,永远不会解释权限的结构。这意味着您无法更改其任何部件,包括端口部件。

以下是从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;

答案 2 :(得分:2)

简单回答:不可能。 如果主机发生更改,则需要使用绝对路径。

答案 3 :(得分:-5)

只需编写href属性:

/:port/[path/]file.ext