未捕获的TypeError:无法在JavaScript函数中读取null(...)的属性“1” [英] Uncaught TypeError: Cannot read property '1' of null(…) in a JavaScript function

查看:266
本文介绍了未捕获的TypeError:无法在JavaScript函数中读取null(...)的属性“1”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方法是模块的一部分;尽管有错误...

This method is part of a module; And despite the error...

未捕获TypeError:无法读取null(...)的属性'1'

在很小程度上起作用,但它似乎阻止了模块上的其他方法。

works to a small degree, however it appears to have blocked an additional method on the module.

这是小提琴,其中包含整个模块。

This is a fiddle which contains the whole module.

searchURL: function() {
  function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  }
  var link, url, parser, newPathName = '',
    newstr = '',
    doc = document,
    avon_rep_container = doc.getElementById('avon_rep_container'),
    avon_rep_container_id,
    avon_rep_container_links,
    avon_rep_container_images,
    documentTableWrapper,
    docBodyFirstChild,
    full_width_container_1 = doc.getElementsByClassName('full-width-container')[1],
    full_width_img_class_el = doc.getElementsByClassName('full-width-img')[0];
  if (!avon_rep_container) {
    avon_rep_container = doc.createElement('div');
    avon_rep_container.setAttribute('id', 'avon_rep_container');
    avon_rep_container.className = 'container-avon-representative-news';
    avon_rep_container_links = avon_rep_container.getElementsByTagName('a');
    avon_rep_container_id = doc.getElementById('avon_rep_container');
    docBodyFirstChild = doc.body.firstChild;
    documentTableWrapper = doc.getElementsByClassName('marginfix')[0];
    avon_rep_container.appendChild(documentTableWrapper);
    doc.body.appendChild(avon_rep_container);
    full_width_container_1.removeChild(full_width_container_1.getElementsByTagName('table')[0]);
    full_width_img_class_el.removeAttribute('style');
  } else {
    avon_rep_container_links = doc.getElementById('avon_rep_container').getElementsByTagName('a');
  }
  avon_rep_container_images = avon_rep_container.getElementsByTagName('img');
  for (var i = 0; i < avon_rep_container_images.length; i++) {
    var images = avon_rep_container_images[i];
    images.src = '/dam/avon-us/landing-pages/rep-news/' + images.src.split('/').pop();
    if (avon_rep_container_images[i].width == "538") {
      avon_rep_container_images[i].style.width = "538px";
    }
    if (avon_rep_container_images[i].width == "258") {
      avon_rep_container_images[i].style.width = "258px";
    }
    avon_rep_container_images[i].style.width = 'inherit';
    avon_rep_container_images[i].style.margin = 'auto';
  }
  //for (var i = 0, len = arguments.length; i < len; i++) { // Using a for loop to allow unlimited arguments to be passed in
  //instead collect all necessary urls
  url = getURL(arguments); //[i]); // We are calling the publicApi.getURL() method and passing the looped argument from above
  for (var j = 0, jlen = avon_rep_container_links.length; j < jlen; j++) { // This loop goes over the whole documents links...
    link = avon_rep_container_links[j];
    var domain = link.href.match(/(https?:\/\/.+?)\//)[1];
    if ((url.indexOf(domain) !== -1) && (!link.href.match(/\.(pdf)/gi))) { // //...and we are checking each argument passed in to see if it matches the object value stored in the getURL function e.g. like a switch statement..
      parser = document.createElement('a'); //...if so we are essentially adding a blank tag to all the documents in the document
      parser.href = link.href;
      link.setAttribute('target', '_self');
      newPathName = parser.pathname;
      console.log(domain);
      if (newPathName.search(/Executive|District|Division|National/) != -1) { // Added check for these strings for SMO page
        newPathName = newPathName.split('/').pop();
        newstr = newPathName;
      } else {
        newstr = newPathName;
      }
      link.href = newstr;
    } else {
      link.setAttribute('target', '_blank');
    }
  }
  //}
}

可以解释一下错误在我的模块的上下文中意味着什么,似乎无法理解它。

Can one explain what the error means in context to my Module, can't seem to understand it.

感谢任何帮助将不胜感激!

Thanks any help will be appreciated!

更新

这是发生错误的地方:

var domain=link.href.match(/(https?:\/\/.+?)\//)[1];


推荐答案

字符串#matc 返回 null (不匹配)或包含匹配项的数组。

String#match returns either null (no match) or an array with the matches.

var domain = link.href.match(/(https?:\/\/.+?)\//)[1];
//                     ^^^^^

检查解决方法

var domain = link.href.match(/(https?:\/\/.+?)\//);

if (domain) {
    // do something with domain[1]
}

这篇关于未捕获的TypeError:无法在JavaScript函数中读取null(...)的属性“1”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆