简体   繁体   中英

javascript if then function - cannot read property

I have the following code on a development page. (Sorry it's inaccessible.) The query parameters on the page are: ?trackid=209B139A-A3BD-4AF0-A596-6248A9F3091C&cat=fafsa%20application

I am expecting to have the value for catKey returned as "fafsa application" but I get the following error in my console:

Uncaught TypeError: Cannot read property 'split' of undefined

Part of the problem is that in most cases, the cat parameter won't be the last one, so I have to split it again on the next ampersand. Because there is not another one, I'm pretty sure that's causing the error. I need to cover all scenarios including this example. But I think there's also something wrong with my syntax or the if-then-else statement.

Hoping one of you will easily tell me what I'm doing wrong.

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);

UPDATED: Based on suggestion I updated the code to the following but am now getting this error:

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=') will equal fafsa%20application but then you further try to split that on &, which isn't valid.

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

You have some other questionable splits in here too, notably the # one and the exclusive one, which I don't see matches to in your example.

That said, these things won't cause that error exactly, but might be compounding the problem. The error "cannot read property split of undefined" would mean that somewhere you're calling it on a variable that isn't quite set yet. Like window. parent .location...?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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