栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript-获取部分URL路径

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript-获取部分URL路径

内置

window.location
对象的一个属性将为当前窗口提供该属性。

// If URL is http://www.somedomain.com/account/search?filter=a#topwindow.location.pathname // /account/search// For reference:window.location.host     // www.somedomain.com (includes port if there is one)window.location.hostname // www.somedomain.comwindow.location.hash     // #topwindow.location.href     // http://www.somedomain.com/account/search?filter=a#topwindow.location.port     // (empty string)window.location.protocol // http:window.location.search   // ?filter=a

更新,对任何URL使用相同的属性:

事实证明,此模式已被标准化为称为 URLUtils的接口,您猜怎么着?现有

window.location
对象 和锚元素均 实现该接口。

因此,您可以对 任何 URL 使用与上面相同的属性 -只需使用URL创建锚并访问属性:

var el = document.createElement('a');el.href = "http://www.somedomain.com/account/search?filter=a#top";el.host        // www.somedomain.com (includes port if there is one[1])el.hostname    // www.somedomain.comel.hash        // #topel.href        // http://www.somedomain.com/account/search?filter=a#topel.pathname    // /account/searchel.port        // (port if there is one[1])el.protocol    // http:el.search      // ?filter=a

这可以在最新版本的Chrome和Firefox中使用 。我没有要测试的Internet
Explorer版本,因此请使用JSFiddle示例进行测试。

还有一个即将到来的

URL
对象,它将提供对URL本身的支持,而没有anchor元素。目前似乎尚无稳定的浏览器支持它,但据说它已在Firefox26中提供。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596827.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号