如何用js获取当前url

// 1、设置或获取整个 URL 为字符串:

  document.write(window.location.href)
  console.log(window.location.href)
  document.write("<br>")

// 2、设置或获取与 URL 关联的端口号码:

  document.write(window.location.port)
  console.log(window.location.port)
  document.write("<br>")

//3、设置或获取 URL 的协议部分

  document.write(window.location.protocol)
  console.log(window.location.protocol)
  document.write("<br>")

// 4、设置或获取 href 属性中跟在问号后面的部分

document.write(window.location.search)
  console.log(window.location.search)
  document.write("<br>")

// 5、获取变量的值(截取等号后面的部分)

 var url = document.location.search;
  document.write(url.length)
  document.write("<br>")
  document.write(url.lastIndexOf('='))
  var loc = url.substring(url.lastIndexOf('=')+1,url.length)
  document.write("<br>")

// 6、设置或获取 URL 的协议部分:

 document.write(window.location.protocol)
  console.log(window.location.protocol)
  document.write("<br>")

// 7、设置或获取 href 属性中在井号“#”后面的分段:

  document.write(window.location.hash)
  console.log(window.location.hash)
  document.write("<br>")

// 8、设置或获取 location 或 URL 的 hostname 和 port 号码:

  document.write(window.location.host)
  console.log(window.location.host)
  document.write("<br>")
发布了227 篇原创文章 · 获赞 41 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42554191/article/details/104871336