- This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
Viewing 8 posts - 1 through 8 (of 8 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › JS basics – how to separate javascript into a separate file
I’m just getting started in making the leap from javascript hack to active learner. Case in point – here’s an old project with javascript written directly into html document. How do I separate this out into its own file? I know how to link from html but I don’t know how to structure the js document.
Any help appreciated!
What do you mean, you don’t know how to structure the JS content? In this case you may just want to copy the JS and paste it all into a new document called something like myScripts.js, or that’s not good?
There’s a shipload of js events inlined to every image.
Frankly, it would probably better to rewrite the thing completely.
Thanks for your comments. Paulie, I think you see what I’m getting at. When you say it would be better to rewrite, do you have an idea about how to approach this? I appreciate your guidance, thanks.
Well I don’t know how it was written in the first place but js (and js libraries) have come along way.
Start from the basics and try not to repeat code whenever possible.
I tried to give an example (since I recommended the separation before) but the code got rejected by the forum. Might be worth having a look at document.getElementById('id')
for outsourcing the script that’s inside the image tags.
The forum really doesn’t like Javascript.
Codepen would be better.
No doubt, it was only two lines of code though (down to one it worked).