External JS file question


Joined
Feb 15, 2021
Messages
7
Reaction score
0
Hi!

my first post here


barely have gotten into JavaScript

i have an .html file with an external js file including
Code:
src="code.js
">

cannot make my

Code:
<script src="code.js">
</script>

work properly


in my codepen, the JS is there, and in my editor the src is correctly named

this is new and this is pretty foreign to me

on a learning curve here : )


help, please
 
Ad

Advertisements

Joined
Nov 27, 2019
Messages
163
Reaction score
24
Don't know how to look at the console in codepen, so I copied this to VScoder and it runs fine. I then changed the three insistence's of console.log to alert and things run fine in codepen.

Give VScoder https://code.visualstudio.com/ a try . It's free and you an do a lot more with it then codepen.
 
Joined
Mar 3, 2021
Messages
56
Reaction score
9
Tested in Firefox and Chrome and the code worked in codepen. If you're trying to run it locally from files on your computer, double-check your paths. Since "code.js" doesn't have a path, it implies that it will look for the JavaScript file in the same directory as the HTML file. Running it locally in Firefox didn't work for me (Content Security Policy error), but Chrome worked, so maybe try a different browser.

I agree with sunfighter, using an IDE will serve you well. Keep at it!
 
Joined
Feb 15, 2021
Messages
7
Reaction score
0
BINGO! IT WORKS!

first, i thank you both for your time and guidance

yes, i am familiar with VS Code. it is my text editor

i took sunfighters lead and changed console.log to alert

changing console.log to alert did it!

ok, some novice questions : )

why does alert work here and not console.log ?

what is the difference between alert and console.log ?

when should i be using alert vs console.log ?

experimenting document.write works... kindly explain why that is

i learned the best way to interact with pages and users is the DOM, i will get there as i learn more

anything else a novice should know in this line of thought?

many thanks!
 
Joined
Nov 27, 2019
Messages
163
Reaction score
24
As I said in my original post I don't know how to use the console i codepen. Maybe they don't have one? But in FireFox when you run your code hit F12 and the console will open and you can see the output of your code - and you could sing alone with it. I was compelled to do so and had to terminate the console at 97 bottles.

A good place to learn about everything you can do with the console in firefox is here https://developer.mozilla.org/en-US/docs/Tools/Web_Console
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
56
Reaction score
9
I'll defer to sunfighter, he seems more fluent in web stuff than I, but I'll try to tackle some of those questions.

Why the console doesn't work is still a mystery, as it works on mine, with the exception of Firefox running the files locally. Did you try other browsers?

alert stops all code execution, so it can be handy to slow things down. But, another big difference is that it's much older. Browsers didn't use to have the console interface at all, let alone a unified API for one, and then it was staggered as to which browsers supported what. That difference should be long irrelevant now, one hopes, but you never know.

I, personally, would stick with console.log for everything unless, as mentioned above, you need to slow down the code near a trouble spot.

document.write inserts stuff directly into the DOM so that you can see it in the browser. That's just another clue in the mystery, I guess. To me, that says that it's not a CSP problem like I had, but is a problem with the developer console. What that problem could be, I've no idea.

Unfortunately, I'm out of the loop in modern web stuff. But, even so, I'd recommend getting a solid grasp of the JavaScript syntax and then learning a framework. The learning curve for each one is about the same as learning JavaScript itself, but it's rare to write a site without one, at least when you're starting out. Start with a client-side library like jQuery UI to build attractive/intuitive user interaction a little easier. Then, to make a site that actually does something, you'll need to dive into server-side. Just so you're only learning one language at a time, I'd stick with server side JS and try Node.js. That's getting into the deep end, but there are a ton of tutorials out there.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top