Don’t know How to Run Javascript ?

Open Source 20 Mar , 2013  


We all know that JavaScript is the world’s most popular programming language as we found in my previous post you can Check It Here. It is being used by many ways like for HTML and the web, for servers, PCs, laptops, tablets, smart phones, and more.

How to Guide

So lets see the concept of How to Run Javascript :

We can run Javascript code in three ways which are following :

1. External Javascript : 

The idea is to  write code in an external file (with a .js extension), which can then be included on our web page and for using it we can have an HTML <script> tag and src attribute to the file’s location in server…

<!-- Code is written in a .js file, then included in the page to use. -->
<script src="/yourfolder/js/example.js"></script>

This is most prominent and recommended way of using js because it will also allow the browser to cache the file on the remote client’s computer which directly decreasing page load time.

2. Inline Javascript :

So after External Javascript second option is to use inline code in the webpage directly… This is also achieved by using HTML tag which is <script> tags, we can easily put our javascript code between the <script> tags..

<!-- Embed code directly in the page using script tags. -->
     alert("Hey this is a popup");

3. Using Attributes :

Now the last option is using event handler attributes of HTML elements. But remember this method is strongly discouraged:

<!-- This code is going to be clicked directly. -->
<a href='javascript:alert("Hello World!");'>Click Me!</a>

So these were the options of using javascript on your web page, This is recomemded that putting script tags to the end of the documents before closing </body> tag so that we can be sure that all elements are defined when script is executed, so try these tips and  Enjoy Javascript !

, , , , ,