简体   繁体   中英

How can i make sure that some js files are loaded before the page continue

Hi i am trying to insert the great syntaxhighlighter into an asp .net page but every time it has a error saying 'dp is undefined'.

I think that it gets to the calling part before it downloads the scripts, how can i make sure the files are loaded before continuing?

Thanks

Doron

Edit: this is the code I use in my aspx file, it works fine as regular HTML but when i try and use in a aspx file it says 'dp is undefined'

<link type="text/css" rel="stylesheet" href="App_Data/Styles/SyntaxHighlighter.css"></link>
    <script type="text/javascript" src="App_Data/Scripts/shCore.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushCpp.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushCSharp.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushCss.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushJava.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushJScript.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushPhp.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushPython.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushRuby.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushSql.js"></script> 
    <script type="text/javascript" src="App_Data/Scripts/shBrushXml.js"></script>
        <script type="text/javascript"> 
        window.onload = function() {
            dp.SyntaxHighlighter.ClipboardSwf = 'http://aaron-mueller.de/vendor/dp_syntax_highlighter/Scripts/clipboard.swf';
            dp.SyntaxHighlighter.HighlightAll('code');
        }
    </script> 

Actually, if putting scripts in right order, everything should work fine:

<script src=".../highlighter.js"></script> 
<script>   
   highlighter.doAnything();
</script>

But it can happen, that the highlighter injects the <script> tags himselves, and in this case Jakub's solution should help: perform everything on document load.

Use either

window.onload = function(){highlighter.doAnything();}

event or (if using jQuery)

$(function(){ 
   highlighter.doAnything();
});

Put the calling part inside

$(function(){
 // your code goes here
})

if you're using jQuery

The HTML page will be executed down the line sequentially; therefore if you have a

<script src="Something.js"></script>

tag up in your page, you need to make sure no code is attempting to call that before you hit the loader. In the case of a dynamically loading set of javascript files, usually there is an injection point where you can insert your callback when loading is finished.

If all else fails, you can always defer execution using setTimeout with a specified time you think that the scripts should be fully loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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