[英]unexpected not defined error in javascript
I have page in which there's a function which calls a function from js file. 我有一个页面,其中有一个函数,该函数从js文件中调用一个函数。
the code which calls function in js file: 调用js文件中的函数的代码:
<script language="javascript" type="text/javascript">
var qc = qc_chat;
window.onload = function()
{
qc.setup('<?php echo $p; ?>');
}
</script>
I am including qc.js file using this code: 我使用以下代码包括qc.js文件:
function doThat() {
$.ajax({
url: 'http://www.example.com',
type: 'GET',
data: 'adrs='+getHost( document.domain ),
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'methodCallback',
success: function( data ) {
if( data.message == "yes" ) {
} else {
$.getScript("qc.js"); //files m including using this ajax
$.getScript("tools.js"); //files m including using this ajax
}
},
error: function( error ) {
console.log( error );
}
});
}
and i am calling doThat()
using <body onload="doThat();">
我正在使用
<body onload="doThat();">
来调用doThat()
but i am getting the error in console Uncaught ReferenceError: qc_chat is not defined
但是我在控制台
Uncaught ReferenceError: qc_chat is not defined
遇到错误Uncaught ReferenceError: qc_chat is not defined
Thanks 谢谢
Since $.getScript
is asynchronous, anything that depends on the script it loads must be done in its callback function. 由于
$.getScript
是异步的,因此任何依赖于其加载的脚本的操作都必须在其回调函数中完成。 So it should be: 所以应该是:
$.getScript('qc.js', function() {
qc_chat.setup('<?php echo $p; ?>');
});
When I can't to edit the place where some modules connects, I use this snippet: 当我无法编辑某些模块的连接位置时,请使用以下代码段:
var waitForqcchat = setInterval(function(){
if(typeof qcchat != 'undefined'){
clearInterval(waitForqcchat);
//here I sure, that module connected
qcchat.setup('<?=$p?>');
}
}, 200);
You should also write a logic to limit waiting if qcchat can never to be connected 您还应该编写逻辑来限制如果无法连接qcchat的等待
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.