InterviewSolution
| 1. |
How to create an object in JavaScript? |
|
Answer» Using the <scrip>t TAG you can store the JavaScript code in an external .js extension file. The tag with the “src” attribute ALLOWS you to include this js file in your HTML. If you are using the same JavaScript code in ALMOST every page, then it’s a good PRACTICE to create an external JS file and include it in HTML of every page. This enhances the loading time. Here’s how you can create external JavaScript file with the extension .js. After creating, add it to the HTML file in the script tag. The ”src” attribute is used to include the external JavaScript file in your HTML: <script src="myfile.js" ></script>Let’s say the following is the content of our external JS file “myfile.js”: function show () { ALERT("Learning is fun!"); }The following is our HTML file, wherein we will include the external file “myfile.js”: <html> <body> <form> <input type="button" value="Click" onclick="show()"/> </form> <script src="show.js"> </script> </body> </html>On running the above “myfile.js” file, the following is visible. Press the button “Click”: On clicking above, the following alert is visible which we added in the external file by creating a JavaScript function: |
|