Jump to content

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

jumbo9i0

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 comment
Share on other sites

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 comment
Share on other sites

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 either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

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 comment
Share on other sites

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

×