42-JavaScript Techniques
42-JavaScript Techniques
42-JavaScript Techniques
=====================
3. Hard to Extend
5. Less Secured
8. Lot of coding
9. Heavy on application
10. Slow
Inline JavaScript
=============
- JavaScript functions are written with in the HTML tag.
- Faster in execution.
- But can't reuse.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>Inline JavaScript</title>
</head>
<body>
<h2>Your Ticket - Click Print Button</h2>
<button onclick="window.print()">Print</button>
</body>
</html>
2. Embedded Script
- In this technique the script is defined in head or body section by using <script>
element.
- You can reuse across various elements.
Ex:
<!DOCTYPE html>
<html>
<head>
<title>Inline JavaScript</title>
<script>
function PrintPage(){
window.print();
}
</script>
</head>
<body>
<h2>Your Ticket - Click Print Button</h2>
<button onclick="PrintPage()">Print</button>
<button onclick="PrintPage()">Print Page</button>
</body>
</html>
<script type="text/javascript">
</script>
<script language="javascript">
</script>
Ex:
<!DOCTYPE html>
<html>
<head>
<title>Inline JavaScript</title>
<script type="text/javascript">
function PrintPage(){
window.print();
}
</script>
</head>
<body>
<h2>Your Ticket - Click Print Button</h2>
<button onclick="PrintPage()">Print</button>
</body>
</html>
"use strict";
var x ;
x = 10; // good
<!--
your code
-->
Ex:
<script>
"use strict";
<!--
var x;
x = 20;
document.write("x=" +x );
-->
</script>
print.js
<script src="print.js"> </script>
Ex:
1. Create a new folder "scripts"
2. Add a file into scripts
"print.js"
"use strict";
function PrintPage(){
window.print();
}
<!DOCTYPE html>
<html>
<head>
<title>Inline JavaScript</title>
<script type="text/javascript" src="../scripts/print.js">
</script>
</head>
<body>
<h2>Your Ticket - Click Print Button</h2>
<button onclick="PrintPage()">Print</button>
</body>
</html>
Syntax:
<body>
<noscript> Please Enable JavaScript </noscript>
</body>