How to include JavaScript file into another
The old versions of Client-side JavaScript offers no built in functions to manage multiple scripts within the browser. But, it is possible to dynamically generate a JavaScript tag and append it to HTML document from inside other JavaScript code.
function msg(){
alert("Import File");
}
Javascript File (next.js)
function msg1(){
alert("Included file");
}
HTML File
<html>
<head>
<script type="text/javascript" src="message.js"></script>
<script>
function includeFile(incFile) {
var e=window.document.createElement('script');
e.setAttribute('src',incFile);
window.document.body.appendChild(e);
}
</script>
</head>
<body onLoad="includeFile('next.js');">
<p>Include a JavaScript file in another JavaScript file</p>
<form>
<input type="button" value="Import File" onclick="msg()"/>
<input type="button" value="Includ File" onclick="msg1()"/>
</form>
</body>
</html>
Here you can see the msg1() function is declared in next.js and not included in HTML file . But with the help of the Javascript function "includeFile(incFile)" you can dynamically generate a JavaScript tag and append next.js to HTML document from inside other JavaScript code.
Related Topics
- JavaScript Popup Boxes
- Opening a new window in JavaScript
- How to Create Drop-Down Lists in JavaScript
- Print the content of a Div using JavaScript/jQuery
- How to get the current URL using JavaScript ?
- How to Detect a Mobile Device with JavaScript/jQuery
- How to validate an email address in JavaScript
- JavaScript Array Iteration
- How to Remove a Specific Item from an Array in JavaScript
- What is JavaScript closures?
- How To Remove a Property from a JavaScript Object
- How to get selected value from Dropdown list in JavaScript
- How do I get the current date in JavaScript?
- How to Open URL in New Tab | JavaScript
- How to delay/wait/Sleep in code execution | JavaScript
- How to round to at most 2 decimal places | JavaScript
- How to convert string to boolean | JavaScript
- How to check undefined in JavaScript?
- How To Copy to Clipboard | JavaScript
- How to encode a URL using JavaScript?
- How to force Input field to enter numbers only | JavaScript
- How to create multiline string in JavaScript
- How to Check for an Empty String in JavaScript?