Reputation: 1
I have a really basic JavaScript project, I'm just testing out and learning the language. All I want the program to do is launch and call a function inside the external main.js file. At the moment nothing happens on launch.
I have tried copying examples given online but they don't seem to be working for me.
HTML File:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Cex_Program</title>
<link href="css/default.css" rel="stylesheet" />
</head>
<body>
<div>Content goes here!</div>
<script src="js/main.js"></script>
<script>
Main();
</script>
</body>
</html>
JavaScript file:
Content of function is irrelevant, I just want the program flow to enter the function (hit my breakpoint);
// Your code here!
var InfiniteLoop = 1;
var DeltaTime = 0;
function Main()
{
var Day = new Date();
var StartTime = Math.round(new Date() / 1000);
var StartCurrentLoopTime = 0;
var EndCurrentLoopTime = 0;
}
Upvotes: 0
Views: 453
Reputation: 4312
<!DOCTYPE html>
<html>
<body>
<div>Content goes here!</div>
<script>function Main( str ) { console.log( str ); }</script>
<script>
Main( "Main!" );
</script>
</body>
</html>
We can deduce from this that either js/main.js
is not being downloaded, the function Main()
is inaccessible to a <script>
outside it or that Main()
simply doesn't function or produce a discernable result.
See this w3schools tutorial about scopes for more details on why Main()
might be inaccessible.
Check your browser's console for errors in the case of Main()
not functioning.
I will happily update this answer as needed in response to positive feedback.
Main()
is accessibleCheck if Main()
is a global function by finding out if window.hasOwnProperty( "Main" )
before typing to use it.
<!DOCTYPE html>
<html>
<body>
<div>Content goes here!</div>
<script>
( function() {
function Main( str ) { console.log( str ); }
} () );
function Alt( str ) { console.log( str ); }
</script>
<script>
if ( window.hasOwnProperty( "Main" ) ) {
Main( "Main!" );
} else {
console.error( "Main() can't be accessed from here" );
if ( window.hasOwnProperty( "Alt" ) ) {
Alt( "Alt!" );
}
}
</script>
</body>
</html>
Upvotes: 3
Reputation: 945
I created your setup on my dev machine and loaded the html page. Assuming the files are named correctly on your machine, I can confirm that the code did execute.
A good way to check if JavaScript is running correctly in the browser is to use a debugger:
Upvotes: 0