Javascript current url

Javascript current url

本文关键字:url current Javascript      更新时间:2023-09-26

我使用的是javascript。如何获取当前URL的路径并将其分配给我的代码?这是我的代码:

$(document).ready(function() {
  $(".share").hideshare({
    link: "current_url",
    position: "top"
  });
});

尝试window.location.href

$(document).ready(function() {
  $(".share").hideshare({
    link: window.location.href,
    position: "top"
  });
});

尝试使用window.location.hrefdocument.URL对象

$(document).ready(function() {
  $(".share").hideshare({
    link: window.location.href,
    position: "top"
  });
});

应该使用以下方法:

$(document).ready(function() {
  $(".share").hideshare({
    link: location.href
    position: "top"
  });
});

window.location(也可以被location引用)属性包含许多与当前页面相关的实用程序函数。

例如锚的window.location.hash或查询字符串的window.location.search

如果不需要完整的url,则可以使用window.location.hrefwindow.location.path表达式。

如果不需要完整的url,请使用window.location.hrefwindow.location.pathname表达式。我是说

$(document).ready(function() {
  $(".share").hideshare({
    link: "window.location.href/window.location.pathname",
    position: "top"
  });
});

要获取当前URL,可以使用:

var pathname = window.location.pathname; // Returns path only
var url      = window.location.href;     // Returns full URL