Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change port of an URL?

CURRENT STATUS

  • I am serving a js-file and an html-page with tomcat on port 8443.
  • I am using the same js file for many domains.

REQUEST

  • As js file is common for all the webapps I have, I want to move the js file apache server on port 9443.
  • Is it possible to cahange the port on the ‘src’ parameter, without knowing the domain-name?

JS-FILE (https://localhost:8443/app-name/index.js -> https://localhost:9443/app-common/index.js)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

console.log('Hellow World');

HTML-PAGE (https://localhost:8443/app-name/index.jsp)

<!doctype html>
<html lang="tr" dir="ltr">
    <head>
        <meta charset=utf-8>
    </head>
    <body style="background-color:black;">
        <script src="index.js"></script>
    </body>
</html>

>Solution :

It’s possible to dynamically change the port in the src attribute of the <script> tag in your HTML page without knowing the domain name. You can achieve this by using JavaScript to modify the src attribute based on the current location’s protocol and hostname.

Here’s how you can do it:

<!doctype html>
<html lang="tr" dir="ltr">
<head>
    <meta charset=utf-8>
</head>
<body style="background-color:black;">
    <script>
        // Get the current protocol and hostname
        var protocol = window.location.protocol;
        var hostname = window.location.hostname;

        // Construct the new URL with the updated port
        var newSrc = protocol + '//' + hostname + ':9443/app-common/index.js';

        // Create a new script element
        var scriptElement = document.createElement('script');

        // Set the src attribute to the new URL
        scriptElement.src = newSrc;

        // Append the script element to the body
        document.body.appendChild(scriptElement);
    </script>
</body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading