Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

in what situation programmer will code in between script instead of outside the script?

Not sure what you mean by "in between the script" and "outside the script", but the image you posted is of "inline" javascript. Similarly to the way you can write CSS in a <style></style> tag or attach it from an external file, you can write javascript directly in the HTML by enclosing it in a <script></script> tag, or you can write it in an external file and link it in a <script> tag, eg

<script type="text/javascript" src="script.js"></script>

and put the javascript code in the script.js file.

Link to post
Share on other sites
2 hours ago, guitargirl15 said:

Not sure what you mean by "in between the script" and "outside the script", but the image you posted is of "inline" javascript. Similarly to the way you can write CSS in a <style></style> tag or attach it from an external file, you can write javascript directly in the HTML by enclosing it in a <script></script> tag, or you can write it in an external file and link it in a <script> tag, eg


<script type="text/javascript" src="script.js"></script>

and put the javascript code in the script.js file.

ops sorry about that, i think i should have said why write html code in javascript(file) when you can write html code in html(file)

Link to post
Share on other sites
4 hours ago, jumbo9i0 said:

ops sorry about that, i think i should have said why write html code in javascript(file) when you can write html code in html(file)

Usually if you want to dynamically generate HTML (e.g. different HTML based on some internal state). Or in this case maybe to give some more context to the date value.

Remember to quote or @mention others, so they are notified of your reply

Link to post
Share on other sites
  • 2 weeks later...

Adding to the thread, I would argue that there is very little difference between inline vs in a seperate file besides organisation.

 

Having things in separate files can make it easier to debug and reuse code. And make files smaller and easier to navigate.

 

Generally, I would include all of my JS and CSS in separate files. Unless the code snippet is quite small and is specific to the current file.

Link to post
Share on other sites

In trivial applications such as this, there isn’t any reason to separate your JavaScript into its own file.

 

However, once you start to work with more advanced applications, particularly where the design follows MVC principles or uses frameworks, it becomes unjustifiable (though not strictly necessary in most cases) to embed all of your JavaScript in your HTML.

 

Another case is where you’re writing TypeScript, which is a superset of JavaScript and cannot be directly embedded into HTML.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×