开发者

Relative URL to a different port number in a hyperlink?

开发者 https://www.devze.com 2023-03-06 16:54 出处:网络
Is there a way without Javascript / server-side scripting to link to a different port number 开发者_Python百科on the same box, if I don\'t know the hostname?

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

e.g.:

<a href=":8080">Look at the other port</a>

(This example does't work as it'll just treat :8080 as a string I want to navigate to)


How about these:

Modify the port number on click:

<a href="/other/" onclick="javascript:event.target.port=8080">Look at another port</a>

However, if you hover your mouse over the link, it doesn't show the link with new port number included. It's not until you click on it that it adds the port number. Also, if the user right-clicks on the link and does "Copy Link Location", they get the unmodified URL without the port number. So this isn't ideal.

Here is a method to change the URL just after the page loads, so hovering over the link or doing "Copy Link Location" will get the updated URL with the port number:

<html>
<head>
<script>
function setHref() {
document.getElementById('modify-me').href = window.location.protocol + "//" + window.location.hostname + ":8080/other/";
}
</script>
</head>

<body onload="setHref()">
<a href="/other/" id="modify-me">Look at another port</a>
</body>
</html>


You can do it easily using document.write and the URL will display correctly when you hover over it. You also do not need a unique ID using this method and it works with Chrome, FireFox and IE. Since we are only referencing variables and not loading any external scripts, using document.write here will not impact the page load performance.

<script language="JavaScript">
document.write('<a href="' + window.location.protocol + '//' + window.location.hostname + ':8080' + window.location.pathname + '" >Link to same page on port 8080:</a> ' );
</script>


It would be nice if this could work, and I don't see why not because : is a reserved character for port separation inside the URI component, so the browser could realistically interpret this as a port relative to this URL, but unfortunately it doesn't and there's no way for it to do that.

You'll therefore need Javascript to do this;

// delegate event for performance, and save attaching a million events to each anchor
document.addEventListener('click', function(event) {
  var target = event.target;
  if (target.tagName.toLowerCase() == 'a')
  {
      var port = target.getAttribute('href').match(/^:(\d+)(.*)/);
      if (port)
      {
         target.href = window.location.origin;
         target.port = port[1];
      }
  }
}, false);

Tested in Firefox 4

Fiddle: http://jsfiddle.net/JtF39/79/


Update: Bug fixed for appending port to end of url and also added support for relative and absolute urls to be appended to the end:

<a href=":8080/test/blah">Test absolute</a>
<a href=":7051./test/blah">Test relative</a>


Modify the port number on mouseover:

<a href="/other/" onmouseover="javascript:event.target.port=8080">Look at another port</a>

This is an improvement of https://stackoverflow.com/a/13522508/1497139 which doesn't have the draw back of not showing the link correctly.


This solution looks cleaner to me

<a href="#"  
    onclick="window.open(`${window.location.hostname}:8080/someMurghiPlease`)">
    Link to some other port on the same host
  </a>


Without JavaScript, you'll have to rely on some server side scripting. For example, if you're using ASP, something like ...

<a href="<%=Request.ServerVariables("SERVER_NAME")%>:8080">Look at the other port</a>

should work. However, the exact format will depend on the technology you are using.


After wrestling with this I found actually that SERVER_NAME is a reserved variable. So, if you are on page (www.example.com:8080) you should be able to drop the 8080 and invoke another port. For instance this modified code just worked for me and moves me from any base port to port 8069 (replace your port as required)

<div>
    <a href="http://<?php print
    $_SERVER{'SERVER_NAME'}; ?>:8069"><img
    src="images/example.png"/>Example Base (http)</a>
</div>


It's better to get the url from the server variables:

// PHP:
<a href="<?=$_SERVER['SERVER_NAME']?>:8080/index.php">

// .net:
<a href='<%=Request.ServerVariables('SERVER_NAME')%>:8080/index.asp'>


No need of complicated javascript : simply insert a script node after your anchor, then get the node in javascript, and modify its href property with the window.location.origin method.

 <a id="trans">Look at the other port</a>
 <script>
      document.getElementById('trans').href='http://'+window.location.origin+':8081';
 </script>

The id property must be unique page wide, so you may want to use other method to retrieve node objects.

Tested with apache and Firefox on Linux.


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.: <a href=":8080/test">Test</a>
    var port = a.getAttribute('href').match(/^:(\d+)(.*)/);
    //port -> ['8080','/test/blah']
    if (port) {
      a.href = port[2]; //a -> <a href="/test">Test</a>
      a.port = port[1]; //a -> <a href="http://localhost:8080/test">Test</a>
    }
  }
}

Call the above function on page load.

or on one line:

function fixPortUrls(){var na=document.querySelectorAll('a[href]');for(var i=0;i<na.length;i++){var a=na[i];var u=a.getAttribute('href').match(/^:(\d+)(.*)/);u&&a.href=u[2]&&a.port=u[1];}}

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


None of the answers I looked at (and I will say, I didn't read them all) adjust the URL such that a middle click or "open in new tab" would function properly -- only a regular click to follow the link. I borrowed from Gary Greene's answer, and instead of adjusting the URL on-the-fly, we can adjust it when the page loads:

...
<script>
function rewriteRelativePortUrls() {
    var links = document.getElementsByTagName("a");
    for (var i=0,max=links.length; i<max; i++)
    {
        var port = links[i].getAttribute("href").match(/^:(\d+)(.*)/);
        if (port)
        {
            newURL = window.location.origin + port[0]
            links[i].setAttribute("href",newURL)
        }    
    }
}

</script>
<body onload="rewriteRelativePortUrls()">
...


I also needed the same functionality, but I wanted to also do protocol replacement.

My solution will look for data-samehost-port or data-samehost-protocol and regenerate the href attribute on the anchor tag to use the same current hostname with the specified port and/or protocol.

Here is what I'm using:

...
<script type="text/JavaScript">
/**
In http you cannot make a relative link to the same domain but different port.
This script will take urls that look like this:
[given current url: 'http://some-domain.com:3000/a/path/file.html']
    1. <a data-samehost-port="8080" href="/some/path">some link</a>
    2. <a data-samehost-protocol="https" href="/some/path">some link</a>
    3. <a data-samehost-protocol="https" data-samehost-port="8080" href="/some/path">some link</a>
and make them look like this:
    1. <a href="http://some-domain.com:8080/some/path">some link</a>
    2. <a href="https://some-domain.com/some/path">some link</a>
    3. <a href="https://some-domain.com:8080/some/path">some link</a>
**/
document.addEventListener('DOMContentLoaded', function() {
    [... new Set([].concat(
        Array.from(document.querySelectorAll('[data-samehost-port]')),
        Array.from(document.querySelectorAll('[data-samehost-protocol]'))    
    ))]

    Array.from(document.querySelectorAll('[data-samehost-port]')).forEach(e=>{
        let port = '80'
        let path = e.getAttribute('href')
        const {hostname,protocol} = document.location
        if(e.hasAttribute('data-samehost-protocol')){
            protocol = e.getAttribute('data-samehost-protocol')
            e.removeAttribute('data-samehost-protocol')
        }
        if(e.hasAttribute('data-samehost-port')){
            port = e.getAttribute('data-samehost-port')
            e.removeAttribute('data-samehost-port')
        }
        if(port==='80') port=''
        e.href = `${protocol}//${hostname}${port?`:${port}`:''}${path}`
    })
})
</script>
...

0

精彩评论

暂无评论...
验证码 换一张
取 消