I have Load() function in external file called cookieRedirect.js and I want to load that function before tag. I used to load it like this:
<script type="text/javascript" src="cookieRedirect.js"></script>
</head>
<body onload=Load();>
But when I try this it's not working:
<script type="text/javascript" src="cookieRedirect.js">
window.onload=Load();
</script>
</head>
<body>
If you want to load that function before page load, just add script code to <head>
of the page, not in the body. window.onload
or onload=""
will execute your code only when page was loaded, not before.
Do it this way :
<html>
<head>
<script src="cookieRedirect.js"></script>
<script>
Load();
</script>
<head>
<body>
</body>
</html>
You can't include a src
attribute in your script
tag and inline JavaScript and expect them both to execute. If you do that, just the src
attribute will execute. See http://ejohn.org/blog/degrading-script-tags/
You can split them into separate tags like so:
<script src="cookieRedirect.js">
</script>
<script>
window.onload='Load';
</script>
If you want Load()
to execute before the HTML document is loaded, you can do this:
<script src="cookieRedirect.js">
</script>
<script>
Load();
</script>
But do note that it will block parsing and rendering of your HTML until Load()
finishes. So if Load()
might take some time then this could have a negative impact on performance. This is unlikely to be a problem for a redirect script but something to bear in mind generally.
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.