Dynamically create link to cups printer service from the index server page on dinamic ips - McKAY brothers, multimedia emulation and support

About McKAY's blog

ads

Post Top Ad

Your Ad Spot

2024/06/07

Dynamically create link to cups printer service from the index server page on dinamic ips


This description is too short so we can summarize this tutorial in several points:

  • obtain the server's IP from the index with javascrip, because its IP is dynamic
  • try to modify the text value of a web page at a specific point
  • It tries to create the value of a link when the page loads, or of any html tag
  • modify the attribute of any html nu tag when loading the page with javascrip
  • modify the attribute of an html tag when performing a mouse action before clicking


Dynamically create the index with a link for the daruma cups printserver, create the link using javascrip and changing the tag !a! link!

First, the base code, we create the index.html file with the touch index.html command, the content then we paste it manually and it will be this:

<html>
    <head>
    </head>
    <body>
       <a id="cups" href="http://IPESTATICO:631">http://ESTATICOIP:631</a>
    </body>
</html>

What is the problem? there is the link!

The problem is that this machine has a dynamic IP, and the IPESTATIC part, which would be the IP, would change when the machine was turned off!

The next time I turn on that IP it will not have the same one (here set as IPESTATIC), and for that we are going to obtain it dynamically and write it dynamically on each restart in that html page, but using javascrip!

part 1 get the ip and direct link

This is easy, there is a direct javascrip code, and it is location.host. NOTE: We do not want the IP of the client who visits the page, but rather that of the server to generate the html and the dynamic link from the IP that will always change from the server!

So we create a code that provides that part:

<script  type="text/javascript">
  function myFunctionCups() {
    linkcups = "http://"+location.host+":631";
    console.log(linkcups);
  }
</script>

You can see the output:



For this, the most reliable method is to use the "id" attribute since the element object needs to be passed by property.

The trick is that once we have obtained the reference (the name of the id) we can modify the "href" attribute and thus insert the new modified link generated automatically with the changed IP!!

<script  type="text/javascript">
  function myFunctionCups() {
    var link = document.getElementById("cups");
    link.getAttribute("href");
    link.setAttribute("href", "atributo cambiado");
    link.textContent = "texto cambiado;
    console.log(link.textContent);
  }
</script>
<body>
  <a id="cups" href="enlace">textooriginal</a>
</body>

The result is that the text is changed from "original text" to "changed text" and more hidden the href attribute will change to "changed attribute" that had "link"



Part 3 use the link in the obtained tag and modify it

Now we join part 1 into a variable, and use that variable in part 2

Basically, in the second code we put the variable from the first code, thus creating the modified tag with the link!


Final code

:

<html>
  <head>
    <script  type="text/javascript">
      function myFunctionCups()       {        linkcups = "http://"+location.host+":631"        var link = document.getElementById("cups");        link.getAttribute("href");        link.setAttribute("href", linkcups);        link.textContent = linkcups;      }    </script>
  </head>
  <body onload="myFunctionCups()" >
     <a id="cups" class="DIR" href="enlacecambiar">texto cambiar</a>
  </body></html>

</html> 

No hay comentarios.:

Publicar un comentario

no stupid winbuntu users allowed!

Entradas populares

Post Top Ad

Your Ad Spot