HTML DOM domain 属性
htmlweb developmentfront end technology更新于 2025/9/24 4:28:17
HTML DOM domain 属性用于获取当前执行文档的服务器的域名。它以字符串形式返回域名,如果未识别域名,则值为 null。
语法
以下是 domain 属性的语法 −
document.domain
示例
让我们看一个 HTML DOM domain 属性的示例 −
<!DOCTYPE html>
<html>
<style>
<h1>domain property example</h1>
<p>Get the domain name property value by clicking the below button </p>
<button onclick="getDomain()">Get domain</button>
<p id="Sample"></p>
<script>
function getDomain() {
var doc = document.domain;
document.getElementById("Sample").innerHTML ="The domain name of the server running
this document is "+doc;
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"获取域名"按钮 −

我们首先创建了一个"获取域名"按钮,当用户点击该按钮时,它将执行 getDomain() 方法 −
<button onclick="getDomain()>获取域名</button>
getDomain() 函数获取文档的 domain 属性,并将其值赋给变量 doc。然后,此变量 doc 显示在与其关联的 id 为"Sample"的段落中,并使用 innerHTML 属性显示预期文本 −
function getDomain() {
var doc = document.domain;
document.getElementById("Sample").innerHTML ="The domain name of the server running this document is "+doc;
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

