javascript如果然后功能 - 无法读取属性

时间:2016-02-01 19:50:28

标签: javascript function variables if-statement

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

我希望将catKey的值作为" fafsa application"但是我的控制台出现以下错误:

  

未捕获的TypeError:无法读取属性' split'未定义的

部分问题在于,在大多数情况下,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>

1 个答案:

答案 0 :(得分:1)

window.parent.location.search.split(&#39; cat =&#39;)将等于fafsa%20application但是你会进一步尝试将其拆分为&amp;,这是无效的。< / p>

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

你也有其他一些可疑的分裂,特别是#one和独家分裂,我在你的例子中看不到匹配。

尽管如此,这些事情确实不会导致错误,但可能会使问题复杂化。错误&#34;无法读取undefined&#34;的属性拆分意味着你在某个尚未完全设定的变量上调用它。喜欢window。 parent .location ...?

Here is a nice answer that shows a cleaner way to handle query string parameters in javascript.