If I want to reference a Javascript script in my HTML page, then do I
If I need my HTML page to use some Javascript, do I reference the Javascript script by adding reference to it inside the <head></head> elements and then the <script></script> elements?
Fwiw, I any want the script to apply to this specific HTML page.
If my HTML/PHP page refers to Javascript code using the <script></script> element, then does that guarantee that that Javascript code will not fire unless the calling HTML/PHP page calls it?
As far as your JS code being “called by other pages” - yes, by default your .js file is accessible to the internet. So not only can your own pages link to it via the script line, but I could link to it as well, if I wanted. From a security perspective, it should be treated the same way as your html files, that is, it is open for everyone to see.
But if I did include a script line where I included your JS code, what would happen? Well, to continue your example, if my web page happened to have a “Close” link, and was formatted like yours, then there’s a possibility the JS code would stop my videos just like it would stop yours. That usage is not going to affect your own users or web pages, only mine. So in theory you really shouldn’t care or know that I used it.
Yes, you can protect your js pages from me using them if you want.
Pardon my dumb questions, but I’m just trying to solve a simple problem - in another thread - and flying blind here.
I actually wasn’t asking but security, but now it looks like I have another thing to worry about!
I was concerned that another script of mine might fire off the Javascript unintentionally. So I was wondering if there was a way I can save Javascript code in a file but just make it so a given script (e.g. “photo-gallery.php”) can use it.
One way to pretty much guarantee you don’t somehow accidentally fire off that JS code is to literally include the source code in the script section, rather than referencing the code from a separate .js file. If you do that, you’ve limited the scope of that JS code to the one html file where you have the script included.
I am trying to apply a simple example that I found over at MDN, but have LOTS of questions not knowing any Javascript. And what we are talking about in this thread is one of them.
But maybe talking about my larger goal will give you some context to help?