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)
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>