María María - 11 months ago 112
HTML Question

JavaScript doesn't run in any browser & is enabled - Have replaced document.write for alert and still wouldn't work

I'm trying this seemingly simple snippet where the only var 'numero' must return a random number as if it were a dice so the range must go from 1 to 6 and it works BUT it only runs when I insert the whole JavaScript code inside the HTML like a

script
like this:



<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>JavaScript Dice</title>
<script type="text/javascript">
var numero = Math.random();
numero = numero * 6;
numero++;
numero = Math.floor(numero);
document.write("Random dice number is: " + numero + "<br><br>")
</script>
</head>

<body>

<p>This is the main</p>

</body>

</html>





Although, I get a lot that I must not use
document.write();
since it's a bad practice (It makes it run slowly or something? I'm still reading my way up to it) and I've tried applying the solutions I've seen in other questions but they're so specific I felt I would've messed up because were hard to implement with such specific elements. So, please, what could I use instead of
document.write
in this case?

MY BIG QUESTION IS why the code below DOESN'T work?


  • My first attempt was to link a JavaScript file from HTML as in:





// The Math Object
var numero = Math.random();
numero = numero * 6;
numero = Math.floor(numero);
numero++;
document.write("Random number: " + numero + "<br><br>");

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>JavaScript dice</title>
<script src="MathObject.js" language="javascript" type="text/javascript">
</script>
</head>

<body>
<p>This is the main </p>
</body>

</html>





THIS DOESN'T WORK IN ANY BROWSER - I've checked if JavaScript is enabled, I've restarted settings, I've tried Chrome, Edge, Internet Explorer, it only works in the Split/Live view of the code inside DreamWeaver CC 2017 itself which is of no use but I know the code works
Which just returns:This is the main.

I even replaced the
document.write
with an alert message just to test it more and it DIDN'T work either unless I run it inside of DreamWeaver or the code editor in here.



// The Math Object
var numero = Math.random();
numero = numero * 6;
numero = Math.floor(numero);
numero++;
alert("Random number: " + numero);





I went crazy trying to seek a solution but decided to implement all JavaScript in HTML. There must be a solution for this or a more appropriate way since I need this to be printed on the document.

TO CLARIFY:
- JavaScript only works when it's completely inside the HTML like a script
- I don't want to print many numbers or a loop. Just one.
- How can I print something from JavaScript on the document without having to write everything on the HTML
Any help with all this, please? Thanks in advance ;)

Answer Source

This sounds like an issue regarding file reference. If you check your F12 Developer Tools, it's probably telling you that it can't find the file MathObject.js.

On your line <script src="MathObject.js" language="javascript" type="text/javascript">, you are stating that MathObject.js is in the same folder as the page you are including it on.

You may simply have forgotten to include it in the project, but it's more likely that you referenced it incorrectly. Depending on where your MathObject.js file actually resides, you probably need to change this reference.

  • You can use the relative link ../MathObject.js to navigate up one folder.
  • You can use the root-relative link /MathObject.js to always reference the root.
  • You can use the absolute link https://example.com/MathObject.js to load a hosted version.

For further reading on relative and absolute URLs, I recommend checking out this Coffee Cup article.

Hope this helps! :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download