简体   繁体   English

更改标签href取决于window.location.href

[英]changing a tag href depends on window.location.href

I try to create a jQuery snippet which detect the window's url and depends on the window's href , change the a tag 's href. 我尝试创建一个jQuery代码段,该代码段检测窗口的url并取决于窗口的href ,更改标记的href。

My code is so far: 到目前为止,我的代码是:

(function($) {
  "use strict";
  $(document).ready(function() {
    $(window).on('load', function() {
      var changeLink = document.getElementById("LinkTag");
      console.log('it is running');
      if ($(window).location.href() == "pageUrl") {
        changeLink.href = "website1";
      } else {
        changeLink.href = "website2";
      }
    });
  });
})(jQuery);

Thanks 谢谢

A number of things going on here: 这里发生了很多事情:

  1. location.href is not a jQuery method; location.href不是jQuery方法; it's a property of the regular window object. 它是常规窗口对象的属性。 Use window.location.href instead of $(window).location.href() . 使用window.location.href代替$(window).location.href()
  2. It looks like you're trying to match the string "pageUrl" instead of a variable -- unless that was intended as a sample string in the question, the variable name shouldn't be enclosed in quotes. 看起来您正在尝试匹配字符串“ pageUrl”而不是变量-除非该字符串用作问题中的示例字符串,否则变量名不应用引号引起来。
  3. You probably don't want to match against the full window.location.href , which includes things (such as the protocol, subdomains, hash, and URL parameters) that can vary while still pointing to the same page; 您可能不希望与完整的window.location.href匹配,该文件包含的内容(例如协议,子域,哈希和URL参数)在指向同一页面时可能会有所不同; window.location.pathname is usually a safer bet (assuming you're trying to match a specific page.) window.location.pathname通常是一个比较安全的选择(假设您要匹配特定的页面。)
  4. It's not necessary to wrap your script in an IIFE and a document.ready and a window.onload . 不必将脚本包装在IIFE和document.readywindow.onload I've removed the redundant layers here. 我在这里删除了多余的层。
  5. (Optional) Mixing jQuery methods and vanilla methods can be confusing to maintain, because you have to keep track of which variables contain jQuery objects and which contain references to regular DOM nodes. (可选)将jQuery方法和vanilla方法混合使用可能会造成混淆,因为您必须跟踪哪些变量包含jQuery对象,哪些变量包含对常规DOM节点的引用。 Since you've already got jQuery elsewhere, I swapped out your document.getElementById for its jQuery equivalent here. 由于您已经在其他地方使用了jQuery,因此我在这里将document.getElementById换成了它的jQuery等效项。

 $(document).ready(function() { var pageUrl = "/js"; // This is the path when running in a stackoverflow snippet var changeLink = $("#LinkTag"); if (window.location.pathname == pageUrl) { changeLink.attr("href","//example.com"); } else { changeLink.attr("href","//somethingelse.com"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="LinkTag">Link</a> 

Repalce your $(window).location for just window.location 仅将window.location换成$(window).location .location

if (window.location.href == "pageUrl")
          changeLink.href = "website1";
     else 
        changeLink.href = "website2";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM