首先我想说明一下,我通常不是开发人员。请放纵一下;-)
我维护多个客户的网站,这些网站由供应商托管。但是,您只能从提供商那里获得一个子域,该子域也会显示在地址栏中。 为了显示自己的域名,我想到了将域名重定向到自己的服务器,通过iFrame集成外部网站。
但是,我的服务器并不是主要的网络服务器,而是专为托管一个网站而设计的。因此,Web 服务器始终使用其默认的 index.html 进行响应。
您是否知道如何根据调用的域在此文件中包含相应的 iFrame,并且如果可能还更改标题中的标题? 非常感谢您的帮助!
index.html 目前看起来像这样:
<html>
<head>
<title>Web Client</title>
</head>
<style>
body {
margin: 0;
padding: 0;
}
body, iframe {
width: 100%;
height: 100%;
}
iframe {
border: 0;
}
</style>
<body>
<iframe src="https://sub.domain.tld/"/>
</body>
</html>
最佳答案
您可以使用 javascript 尝试类似的操作:
<html>
<head>
<title>Web Client</title>
</head>
<style>
body {
margin: 0;
padding: 0;
}
body, iframe {
width: 100%;
height: 100%;
}
iframe {
border: 0;
}
</style>
<body>
<iframe id="website" src=""></iframe>
</body>
<script type="text/javascript">
//Get the current URL
var currentURL = window.location.href;
//Get the subdomain from URL
var hostnameURL = currentURL.replace(/(http[s]*\:\/\/)/gi, '').split("/")[0];
//Set the page title
document.title = hostnameURL;
//Transform subdomain to alphaNumeric
var alphaNumericSubDomainName = hostnameURL.replace(/[^\w]/gi, '');
//Set the subdomain to the iframe (don't forget to change the domain name below)
document.getElementById("website").src = "https://"+ alphaNumericSubDomainName + ".domain.tld";
</script>
</html>
我首先获取当前 URL。然后我得到 URL 的主机名(没有 TLD 的域)。
我使用这个主机名来设置标题。然后我删除主机名中的特殊字符并将这个经过清理的主机名连接为子域。
最后,我将这个新 URL 作为 Iframe 的源 URL。
关于html - 是否可以根据寻址域集成 iFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60541620/