简体   繁体   中英

How set href in html <a> tag

Let's I have two server running on my PC. server1 and server2 ....

  1. In server1 there is a html page.(ex. a.html)
  2. In server2 there is another html page.(ex b.html)
  3. Server1 is running on port 80
  4. Server2 is running on port 8080

In a.html of server1 there is a tag. Now I want to access b.html from server2 by the above link that's why I have written

<a href="localhost:8080/b.html">

It's working really well if I access a.html and click that link working fine in my computer where both servers are running.

...... I want to access a.html from server1 in my phone. My pc and phone both are in the same wifi network.

To solve this problem I opened my phone browser and gave my pc ip ex. 192.168.0.1/a.html

It's working fine. But as I said in above that there is a link in that page (ex given above). When I click on that link it's giving error

This site can't be reached localhost refused to connect

.... I know where is the problem . The problem is in the link because I explicitly wrote localhost:8080 . Here is the problem. I should write my pc private it instead of localhost then it'll work I know. But how to get my private IP by javascript to modify the link dynamically by javascript.

Help me to solve this problem.

Your phone is basically redirecting to itself.

Your <a> tag should look like this:

in 192.168.0.1/a.html

<a href="192.168.0.1:8080/b.html">

or if you prefer not to muddy up your HTML then this also possible

in 192.168.0.1/a.html

<HTML5>
    <head>
        <base href="192.168.0.1:8080/">
    </head>
    <body>
        <a href="b.html">b.html on server #2</a>
        <br>
        <a href="192.168.0.1/c.html">c.html on server #1</a>
    </body>
</html>

Both servers are running on your pc. So, to access both servers except own pc you need to provide your private ip. This way you can access your both servers.

Now, you, at first want to access your a.html of server1 where there is a link to b.html of server2.

Don't write localhost explicitly. And you want to change href of the link dynamically. Follw below step.

  1. Change your link of a.html of server1 to <a id="link" href="#"></a>
  2. You have to change your link dynamically. So to change the link I am using JQuery window.onload() method. Add this script to your code

 <script> window.onload=function() { //Modify the link href var ip=location.host; $("a#link").attr("href", "http://"+ip+":8080/b.html"); } </script> 

  1. Because your server2 is running on port 8080 and has a file named b.html
  2. Now you don't need to worry. it'll work on both your pc and another pc or phone in the same network.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM