Add JavaScript onto a web page

Good Morning Guys,
In this article, I share some idea related to javascript. The javascript is a client side programming language. There are serveral methods are available for adding javascript on a web page but there are two way with is commonly used by developers.If your script code is very short and only for single page then following ways is best.
a)You can place <script type=”text/javascript”> tag inside the <head> element.



Code:


Page Title






b).If your script code is very large then you can make a javascript file and add its path in the following way..

Code:


Page Title


The script tag contain some attributes:

  • defer : The script associate with script tag then the browser informs.
  • language : This is used to announce the version of javascript.
  • src: This is used for specify the URLs. The external file is included using src attributes.
  • type : Indicate the content type

Everybody can add javascript functions or script anywhere in HTML or PHP page inside the head or body sections. Here I share some basic guidelines to keep your document well structured.

  • Try to keep your script inside the document head.
  • If your script needs to run at a certain stage during page layout, it should be put at the correct stage of the document.
  • If the script is very small, then put it all where it will be used.
  • If it is larger, then put it inside the head inside a function, and you can then call that function when you need it.

Adding a script to the page

To insert JavaScript into a web page, use the <script> tag. You should use the type attribute to specify the type of script being used, which in the case of JavaScript is text/javascript.




This is an example of a script tag, used to include a script in your page:





If your script is incapable of providing its own fallback, but it is needed to access the page, you should include support for non-JavaScript browsers by using:





Using comments

Comments allow you to leave notes to yourself and other people in your script. The double slash indicates a single line comment.


Using external script files

You can also use a separate header file to contain the JavaScript (usually *.js) making it easy to share across pages




Commenting out your scripts

You can hide the script from them by commenting out your script with standard HTML comments. Browsers that understand script will simply ignore these comments, and will just interpret the script within them. The opening comment is ignored as if it were a normal single line JavaScript comment. The closing comment will not, so it needs to have a JavaScript comment put before it.


Dealing with XHTML





Controlling when the script is activated





Leave a Reply

Your email address will not be published.