Come posso fare in modo che alcuni file js vengono caricati prima che la pagina continua
-
26-10-2019 - |
Domanda
Ciao io sto cercando di inserire la grande SyntaxHighlighter in una pagina ASP .NET, ma ogni volta che ha un errore che dice 'dp è indefinito'.
Credo che si arriva alla parte chiamante prima che scarica gli script, come posso assicurarsi che i file vengono caricati prima di continuare?
Grazie
Doron
Edit: questo è il codice che uso nel mio file aspx, funziona benissimo come normale HTML, ma quando provo e l'uso in un file aspx si dice 'dp è indefinito'
<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>
Soluzione
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();
});
Altri suggerimenti
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.