在iOS平台上使用JavaScript获取当前页面的URL,可以通过以下几种方式:
window.location
对象在所有现代浏览器中都有很好的支持,包括iOS上的Safari。以下是一些常用的方法来获取当前页面的URL:
// 获取完整的URL
var fullUrl = window.location.href;
console.log("Full URL:", fullUrl);
// 获取协议(如http或https)
var protocol = window.location.protocol;
console.log("Protocol:", protocol);
// 获取主机名(如www.example.com)
var hostname = window.location.hostname;
console.log("Hostname:", hostname);
// 获取端口号(如果有)
var port = window.location.port;
console.log("Port:", port);
// 获取路径名(如/path/to/page)
var pathname = window.location.pathname;
console.log("Pathname:", pathname);
// 获取URL的查询字符串(如?key=value)
var search = window.location.search;
console.log("Search:", search);
// 获取URL的哈希部分(如#section)
var hash = window.location.hash;
console.log("Hash:", hash);
window.location
对象。window.addEventListener('load', function() {
console.log("Current URL after load:", window.location.href);
});
通过window.location
对象,你可以轻松获取当前页面的详细URL信息,并根据需要进行相应的处理。在实际应用中,注意处理页面加载时机和跨域问题,以确保获取到的URL信息准确可靠。
领取专属 10元无门槛券
手把手带您无忧上云