0

I am trying to run an external javascript function when the page loads and the function takes a variable that I can only pass through the page which where the javascript function is called from.

HTML Page

<head>
<script src="path/to/file.js" type="text/javscript>
window.onload = function() {
doFunction(variable);
}
</script>
</head>

Javascript file

function doFunction(variable){
//do the stuff here
}
Austin McCalley
  • 392
  • 1
  • 4
  • 17

2 Answers2

2
<head>
<script src="path/to/file.js" type="text/javscript></script>
<script>
$(window).load(function() {
      doFunction(variable);
});
</script>
</head>

The reference script tag(for including external files) should not include any external javascript code in it. you can only reference a external file.

To add any other javascript that should be between seperate script tags

praveen
  • 489
  • 6
  • 12
0

Since you aren't loading the file.js asynchronously, you can include the script in the header. As long as you call the function AFTER the script is loaded, it should work fine.

<html>
<head>
    <script src="path/to/file.js" type="text/javscript>
</head>
<body>
<script type="text/javascript">
    window.onload = function() {
        doFunction('someVal');
    };
</script>
</body>
</html>
Brian Putt
  • 1,288
  • 2
  • 15
  • 33