迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中获取域名

作者:迹忆客 最近更新:2023/06/02 浏览次数:

本文将讨论如何使用 JavaScript 事件和函数在网页执行期间以编程方式获取域名。


域名

我们使用域名从客户端应用程序访问网站或网页。 它是映射到 IP 地址(数字)的文本格式字符串。

用户在 Web 浏览器搜索中键入以调用特定网站的字符串称为域名。 例如,delftstack.com 是 DelftStack 的域名。

例子:

let url = "https://www.jiyik.com/" // jiyik.com is a domain name

假设我们正在开发一个网站或网页。 在某些情况下,我们需要在网站运行期间以编程方式找出并使用当前域名。

在这种情况下,我们可以使用 JavaScript 来处理它。


在 JavaScript 中获取域名

现在,如果我们打开浏览器的控制台并获取对象 window.location.hostname,我们可以看到该页面的主机名是 delftstack.com。

window.location 是 JavaScript 中的一个对象,它可以找到当前页面的 URL 作为字符串,我们可以将浏览器重定向到另一个页面。

属性 window.location.hostname 将返回当前页面的互联网主机名。 如果我们的网页在实时域名托管上,我们可以使用 window.location.hostname 获取 URL。

语法:

let hostname = window.location.hostname

示例:

<!DOCTYPE html>
<html>
<body>

<h1>迹忆客 learnig</h1>
    <h2>JavaScript get domain name example</h2>

<p id="para"></p>

<script>
let result = ''
let removeValue = 'www.'
let domainName = window.location.hostname;
   result = domainName.replace(removeValue,'')


document.getElementById("para").innerHTML =
"Current page hostname is: " + result;
</script>

</body>
</html>

代码解释:

  1. 在上面的 HTML 源代码中,我们使用了段落元素标记 <p></p> 并将 Id 分配给该元素。
  2. <script> 标签内,我们用 window.location.hostname 声明并初始化了 domainName 变量。 我们将获得字符串形式的完整 URL。
  3. 现在,要修剪该字符串以仅查找域,我们已经初始化了变量 removeValue 并分配了 www..
  4. 我们在 JavaScript 默认方法 replace() 中使用了该变量来删除 www. 从 URL 并将最终值存储在结果变量中。
  5. 最后,在 document.getElementById() 方法的帮助下,我们显示了结果字符串。
  6. 您可以使用 HTML 扩展名保存上述源代码并查看结果,确保在活动域上托管该 HTML 文档。

使用本地存储的 HTML 文档

如果您没有实时域名托管,您可以使用 window.location.href 在您的系统上找到本地存储网页的路径。 它将返回 HTML 文档的完整路径。

语法:

let pathValue = window.location.href // file:///C:/Users/username/foldername/filename.html

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便