Jacques Wolfghang
Jacques Wolfghang

Reputation: 77

Check when script has loaded

I am trying to call a function that, on the load of the script above it, will run some JavaScript.

<script src="../Layout/jquery.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript" src="helperscript.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        var itemid = '1';
        if(itemid)
        {
            idselect(itemid);
        }
    })
</script>

helperscript.js contains the function idselect. When I load the page I get an error saying that 'idselect is undefined', even though it is in the above file. I suspect that this is due to helperscript not being fully loaded yet, but that is just a hunch.

Can anyone help me?

Upvotes: 1

Views: 3051

Answers (4)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

Make sure your path to the script to the .js file is correct.

Also cut the language="javascript" part.

Upvotes: 0

Adrian Schmidt
Adrian Schmidt

Reputation: 1885

I don't think your hunch is right, because this script tag:

<script type="text/javascript" language="javascript" src="helperscript.js"></script>

should block any other javascript from executing until helperscript.js is downloaded and executed.

Upvotes: 2

Quentin
Quentin

Reputation: 944246

Scripts load in order and are blocking.

There must be a problem with the loading of the script, not the timing.

Most likely you have either a mistake in the URL (resulting in a 404) or a JavaScript error (which would show up on the error console)

Upvotes: 3

James Kovacs
James Kovacs

Reputation: 11661

Use jQuery.getScript() and register a success function. It will be safe to use idselect on or after the success function is executed.

http://api.jquery.com/jQuery.getScript/

Upvotes: 0

Related Questions