0

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;
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • you need eventListener on window load :) – treyBake Jun 30 '17 at 14:51
  • 1
    @DylanSteele what do you mean? `Main()` is telling it to run. It's just that there's no output, or anything to show that the function has run. – evolutionxbox Jun 30 '17 at 14:59
  • Thanks for the responses. – H Dennis Jun 30 '17 at 18:17
  • Ok enter doesnt paragraph but sets the comment... Anyway i have the breakpoint set before i run the program (im in visual studio) and it never gets hit. I also added the consol.log(day) line as a check and it still doesnt work. (I see no output). – H Dennis Jun 30 '17 at 18:18
  • You need special file structure: [![project root][1]][1] [1]: https://i.stack.imgur.com/DoreL.png – George Jun 30 '17 at 15:19

2 Answers2

3

This does work

<!DOCTYPE html>
<html>
  <body>
    <div>Content goes here!</div>
    <script>function Main( str ) { console.log( str ); }</script>
    <script>
        Main( "Main!" );
    </script>
  </body>
</html>

So

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.

To test if Main() is accessible

Check 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>
Fred Gandt
  • 4,217
  • 2
  • 33
  • 41
  • I'm thinking that `Main` does run. It's just that nothing is being output, nor logged, etc. – evolutionxbox Jun 30 '17 at 15:00
  • 1
    I like being excellent! --- _"produce a discernable result"_ - I'm betting on this. – evolutionxbox Jun 30 '17 at 15:06
  • Ok enter doesnt paragraph but sets the comment... Anyway i have the breakpoint set before i run the program (im in visual studio) and it never gets hit. I also added the consol.log(day) line as a check and it still doesnt work. (I see no output). repeat of comment i made above – H Dennis Jun 30 '17 at 18:21
  • @HDennis - Assuming `js/main.js` is being downloaded correctly (the path is good and the script loads without error), that `js/main.js` is being loaded synchronously, and that `Main()` is accessible (is [global](https://stackoverflow.com/questions/29514382/global-functions-in-javascript)) and functioning correctly; everything **should** be working as my example shows. To simplify your debugging, try removing the breakpoint, commenting out the content of `Main()` and replacing it with only a simple `console.log()`. If that doesn't happen, one or more of the other stated possibilities is likely. – Fred Gandt Jun 30 '17 at 18:37
  • "CSP14312: Resource violated directive ‘script-src ms-appx: 'unsafe-eval'’ in Host Defined Policy: inline script. Resource will be blocked." Is being reported as a warning in the JavaScript Console, does that look related to you? I did try just console.log() but didnt see anything come up as a result of it. why would main.js not be being loaded synchronously? this is just a default new project in visual studio so the include paths and loading should all be defaulted and working i assume? – H Dennis Jun 30 '17 at 19:58
  • Trying out your code all i get in the console window is "Content goes here!", nothing shows up beneath it at all. I wonder if its not a code thing but a strange windows 10 or visual studio thing? – H Dennis Jun 30 '17 at 20:06
  • @HDennis - Ah, well, yes... If `js/main.js` is being blocked, you'll definitely have a problem. Whilst developing, it is a good idea to [set your `Content-Security_Policy` to `-Report-Only`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only). Do that and see if your call of `Main()` works as expected (the policy won't be enforced). Figuring out the conflict will be beyond the scope of this question, and if you need help with it, you should post another with the specifics. – Fred Gandt Jun 30 '17 at 20:06
0

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:

LeDoc
  • 935
  • 2
  • 12
  • 24