
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Importing and Exporting Modules
To import and export modules using JavaScript, the code is as follows −
Note − To run this example you will need to run a localhost server.
INDEX.html
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Importing and Exporting Modules</h1> <button class="Btn">IMPORT</button> <div class="result"></div> <h3> Click on the above button to import module </h3> <script src="script.js" type="module"></script> <script src="sample.js" type="module"></script> </body> </html>
script.js
import test from './sample.js'; document.querySelector('.Btn').addEventListener('click',()=>{ test(); })
sample.js
let resultEle = document.querySelector(".result"); export default function testImport(){ resultEle.innerHTML = 'Module testImport has been imported'; }
Output
On clicking the ‘IMPORT’ button −
- Related Articles
- IMPORTING, EXPORTING and CHANGING Keywords in ABAP
- Importing / Exporting CSV file in PowerShell
- Importing/Exporting ABAP packages to Presentation server
- JavaScript modules
- Loading JavaScript modules dynamically
- What are modules in JavaScript?
- PHP Aliasing/Importing namespaces
- Importing Data in Python
- Exporting models data in Django
- What is the difference between importing matplotlib and matplotlib.pyplot?
- Locating and executing Python modules (runpy)
- Difference Between GBIC and SFP Modules
- Exporting a SAP HANA Modeling view
- Importing External Style Sheets in CSS
- Importing data into models in Django

Advertisements