简体   繁体   English

检查脚本何时加载

[英]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. 我正在尝试调用一个函数,该函数在上面的脚本加载时将运行一些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 . helperscript.js包含功能idselect When I load the page I get an error saying that 'idselect is undefined' , even though it is in the above file. 加载页面时,我收到一条错误消息,说'idselect is undefined' ,即使它在上述文件中也是如此。 I suspect that this is due to helperscript not being fully loaded yet, but that is just a hunch. 我怀疑这是由于辅助脚本尚未完全加载而造成的,但这只是预感。

Can anyone help me? 谁能帮我?

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) 您很可能是URL错误(导致404)或JavaScript错误(将显示在错误控制台上)

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. 在下载并执行helperscript.js之前,应阻止其他任何javascript执行。

Use jQuery.getScript() and register a success function. 使用jQuery.getScript()并注册一个成功函数。 It will be safe to use idselect on or after the success function is executed. 在成功函数执行之后或之后使用idselect是安全的。

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

Make sure your path to the script to the .js file is correct. 确保您的.js文件脚本路径正确。

Also cut the language="javascript" part. 还要剪切language="javascript"部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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