JavaScript 如果然后无法读取属性
javascript if then cannot read property
我在开发页面上有以下代码。(抱歉无法访问。页面上的查询参数为:?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中处理查询字符串参数的更简洁的方法。
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- JavaScript承诺-无法读取属性'那么'的未定义
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- http/rxjs catch回调中的Angular 2重定向导致TypeError:无法读取属性'订阅'
- Javascript未捕获类型错误:无法读取属性'0'的未定义
- jQuery控制台错误:无法读取属性'top'的未定义
- TypeError:无法读取属性'findAll'的未定义