-1

I can't succeed in calling JavaScript file from a HTML file.

test.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script  src='http://code.jquery.com/jquery-3.2.1.min.js'</script> 
    <script type='text/javascript' src='test.js' </script>
    <script>theTest(); </script>
</head>
<body>
    <div id="myTest" style="position:relative;width:600px;height:400px;"></div>
</body>
</html>

test.js:

function theTest()
{
    var they = 2;
    console.log(they);
    alert(they);
};

document.getElementById('myTest').innerHTML;

I am receiving:

ReferenceError: document is not defined

Lidor Avitan
  • 1,374
  • 10
  • 22
George
  • 5,808
  • 15
  • 83
  • 160

1 Answers1

0

You are calling theTest(); before the browser is generating the #myTest element, or even starting to build the page, so the function has nothing to act on. Move your third script tag down to the end of your body element, and it should work fine. See this example:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src='http://code.jquery.com/jquery-3.2.1.min.js'></script> 
    <script type='text/javascript' src='test.js'></script>
</head>
<body>
    <div id="myTest" style="position:relative;width:600px;height:400px;"></div>
    <script>
        theTest();
    </script>
</body>
</html>
freginold
  • 3,946
  • 3
  • 13
  • 28
  • H, thanks but I am still getting the error about document not found.Let me remind you that I want to call the javascript file which executes the html file and not the opposite. – George Sep 11 '17 at 14:01
  • 1
    But the function is not calling any page element. – Junius L Sep 11 '17 at 14:02
  • @George Where are you loading the HTML file from your JavaScript file? – freginold Sep 11 '17 at 14:02
  • @freginold:I am trying to use the `document.getElementById('myTest').innerHTML;` for that.Sorry, I am not experienced in html and javascript. – George Sep 11 '17 at 14:03
  • 1
    @George That line of code does literally nothing. You're getting the HTML content of the `myTest` element, but you're not saving it, changing it, doing anything with it, etc. For example, `document.getElementById('myTest').innerHTML = "Hello world!"` would set the HTML. `var myHtml = document.getElementById('myTest').innerHTML;` would store it as a variable. But that line on it's own has no purpose. – Tyler Roper Sep 11 '17 at 14:04
  • @George To be clear, you're trying to have `test.js` load `test.html`? – freginold Sep 11 '17 at 14:06
  • @freginold:yes, exactly.and of course use any result from javascript file. – George Sep 11 '17 at 14:08
  • @George To load a new HTML page, you would do something like this: `window.location.href = "test.html";` – freginold Sep 11 '17 at 14:20
  • @freginold;It says `window` id not defined, like `document` is not defined.And I can't seem to find a solution1 – George Sep 12 '17 at 06:21
  • @George What command are you running that is giving an "ID not defined" error for `window`? – freginold Sep 12 '17 at 12:09
  • 1
    @freginold:`node test.js` – George Sep 12 '17 at 12:21
  • @George If you're running this code in Node.js, that is completely different from running it in a web browser. Node.js does not have a `window` object. See [this SO answer](https://stackoverflow.com/a/33320084/5463636) for more detail. – freginold Sep 12 '17 at 12:39
  • @freginold:I see..So, there is no easy solution for this.You can't just launch an html file from javascript code. – George Sep 12 '17 at 12:41
  • @George It really depends on what you want to do. If you're using a Windows system and don't actually need a web browser, just a GUI, you could use an [HTA file](https://technet.microsoft.com/en-us/scriptcenter/dd742317.aspx?f=255&MSPPError=-2147217396). – freginold Sep 12 '17 at 12:45