0

I have page in which there's a function which calls a function from js file.

the code which calls function in js file:

<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:

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

but i am getting the error in console Uncaught ReferenceError: qc_chat is not defined

Thanks

11
  • 4
    Where is qc_chat defined. if it is not defined at all you will get this error. Commented Oct 9, 2013 at 18:05
  • 1
    Yes, please show where it is defined. It is not in your code. Commented Oct 9, 2013 at 18:06
  • Is qc_chat by any chance defined inside qc.js? Commented Oct 9, 2013 at 18:06
  • 1
    The onLoad events are going to fire before any of the async calls finish... Commented Oct 9, 2013 at 18:06
  • 1
    You can't use PHP in an external JS file Commented Oct 9, 2013 at 18:07

2 Answers 2

3

Since $.getScript is asynchronous, anything that depends on the script it loads must be done in its callback function. So it should be:

$.getScript('qc.js', function() {
    qc_chat.setup('<?php echo $p; ?>');
});
Sign up to request clarification or add additional context in comments.

11 Comments

may be <body onload="doThat('<?php echo $p; ?>');"> and function doThat(_p) { and qc_chat.setup(_p) since the other code is a .js file
error is not shwoing but my code is also not working...which is dependent on that files. :(
If it's not getting an error, then it's running the function. I can't explain why it's not doing what you expect. Use the JS debugger, set a breakpoint on the qc_chat.setup line and see what's happening.
@Barmar nohting happend...i think i need to load the doThat() before the page loads so that it load qc.js before the page loads completey? how to do that ?
That seems unlikely. But you can but the call do doThat() in a <script> tag in the <head> instead of the onload attribute, and it will run before the body loads. But since it uses AJAX, qc.js will still not be loaded until later.
|
0

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

2 Comments

It works, but it's an incredible kludge. Learn to use callback functions properly.
Agree, this only for cases, when I can't modify/detect the place where script connected. Place like $.getScript('qc.js')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.