JavaScript 如果然后无法读取属性

javascript if then cannot read property

本文关键字:读取 属性 如果 然后 JavaScript      更新时间:2023-09-26

我在开发页面上有以下代码。(抱歉无法访问。页面上的查询参数为:?trackid=209B139A-A3BD-4AF0-A596-6248A9F3091C&cat=fafsa%20application

我希望将catKey的值作为" fafsa应用程序"返回,但是在我的控制台中出现以下错误:

捕获的类型错误:无法读取未定义的属性"拆分"

部分问题在于,在大多数情况下,cat 参数不会是最后一个,所以我必须在下一个 & 符号上再次拆分它。因为没有另一个,所以我很确定这是导致错误的。我需要涵盖包括此示例在内的所有方案。但我认为我的语法或 if-then-else 语句也有问题。

希望你们中的一个人能轻易地告诉我我做错了什么。

function catKey() {
  if (window.parent.location.search.split('cat=')[1] !== null) {
    decodeURIComponent(window.parent.location.search.split('cat=')[1].split('&')[0]);
  } else { }
};
var _eaq = _eaq || [];
var a = {
  'RenderingDiv': 'div-gpt-ad-1439558353478-0', //Id of the element which you want the Ads to render (Required)
  'AdServer': 'DFP', //The Ad Server (Optional: defaults to DFP)
  'Sizes': [960, 1600], //The Ad Sizes which are normally added in the head (Required for Double Click Ads)
  'AdUnitPath': '/59026966/Exit_Pop',  //The Ad unit Value which is normally added in the head (Required for Double Click Ads)
  'Vendor': 'VENDOR1', //The Vendor Name (Optional: VANTAGE default)
  'IsWizard': true, //if IsWizard is true, you can add below line 
  'AddiTargetingParam': {
    'domain': window.parent.location.hostname, //targets the domain
    'landingpage': window.parent.location.pathname.split('/')[2], //targets the landing page directory name
    'step': window.parent.location.href.split('#')[1], //targets the step
    'trackid': document.cookie.split('_CampaignTrackID=')[1].split(';')[0], //pulls trackid out of cookie
    'exclusive_id': document.cookie.split('_CampaignTrackID=')[1].split(';')[0], //pulls trackid out of cookie
    'exclusive': window.parent.location.search.split('exclusive=')[1], //pulls exclusive out of url
    'cat': catKey(),
  }, //Customized additional targeting parameters
};
_eaq.push(a);
更新:

根据建议,我将代码更新为以下内容,但现在收到此错误:

Uncaught TypeError: Method RegExp.prototype.toString called on incompatible receiver [object Object]
<script type='text/javascript'>
       function getParameterByName(name) {name = name.replace(/['[]/, "''[").replace(/[']]/, "'']"); var regex = new RegExp("[''?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results === null ? "" : decodeURIComponent(results[1].replace(/'+/g, " "));}
        var catKey = getParameterByName('cat');
        var _eaq = _eaq || [];
        var a = {
               'RenderingDiv': 'div-gpt-ad-1439558353478-0', //Id of the element which you want the Ads to render (Required)
               'AdServer': 'DFP', //The Ad Server (Optional: defaults to DFP)
               'Sizes': [960, 1600], //The Ad Sizes which are normally added in the head (Required for Double Click Ads)
               'AdUnitPath': '/59026966/Exit_Pop',  //The Ad unit Value which is normally added in the head (Required for Double Click Ads)
               'Vendor': 'MEDIAALPHA', //The Vendor Name (Optional: VANTAGE default)
               'IsWizard': true, //if IsWizard is true, you can add below line 
               'AddiTargetingParam': {
                  'domain': window.parent.location.hostname, //targets the domain
                  'landingpage': window.parent.location.pathname.split('/')[2], //targets the landing page directory name
                  'step': window.parent.location.href.split('#')[1], //targets the step
                  'trackid': document.cookie.split('_CampaignTrackID=')[1].split(';')[0], //pulls trackid out of cookie
                  'exclusive_id': document.cookie.split('_CampaignTrackID=')[1].split(';')[0], //pulls trackid out of cookie
                  'exclusive': window.parent.location.search.split('exclusive=')[1], //pulls exclusive out of url
                  'cat': catKey,
                },//Customized additional targeting parameters
         };
        _eaq.push(a); 
</script>

window.parent.location.search.split('cat=') 将等于 fafsa%20application,但随后您进一步尝试将其拆分为 &,这是无效的。

(window.parent.location.search.split('cat=')[1].split('&')[0])

您在这里还有其他一些可疑的拆分,特别是#一和排他性,我在您的示例中没有看到匹配项。

也就是说,这些事情不会完全导致该错误,但可能会使问题复杂化。错误"无法读取未定义的属性拆分"意味着您在某个地方正在对尚未完全设置的变量调用它。像窗户。父位置...?

这是一个很好的答案,它显示了在javascript中处理查询字符串参数的更简洁的方法。