Relative URL to a different port number in a hyperlink?

前端 未结 11 1879
心在旅途
心在旅途 2020-11-27 15:13

Is there a way without Javascript / server-side scripting to link to a different port number on the same box, if I don\'t know the hostname?

e.g.:

&l         


        
11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 15:56

    Based on Gary Hole's answer, but changes urls on page load instead of on click.

    I wanted to show the url using css:

    a:after {
      content: attr(href);
    }
    

    So I needed the anchor's href to be converted to contain the actual url that would be visited.

    function fixPortUrls(){
      var nodeArray = document.querySelectorAll('a[href]');
      for (var i = 0; i < nodeArray.length; i++) {
        var a = nodeArray[i];
        // a -> e.g.: Test
        var port = a.getAttribute('href').match(/^:(\d+)(.*)/);
        //port -> ['8080','/test/blah']
        if (port) {
          a.href = port[2]; //a -> Test
          a.port = port[1]; //a -> Test
        }
      }
    }
    

    Call the above function on page load.

    or on one line:

    function fixPortUrls(){var na=document.querySelectorAll('a[href]');for(var i=0;i

    (I'm using for instead of forEach so it works in IE7.)

提交回复
热议问题