hai

i want declare

imports -----header file in javascript

<script>
//// in this section how to declare imports
<script>

please help me

Dani AI

Generated

there is no concept of a C/C++-style "header file" in JavaScript. What you want is either an external script or, better today, an ES module. Load a module and import what you need like this:

<!-- HTML -->
<script type="module" src="/js/app.js"></script>
// /js/app.js
import {format} from "./utils/date.js";
console.log(format(new Date()));

Module scripts are deferred by default, so the page parses first and then runs your code. Unlike classic scripts, import works only inside modules. (developer.mozilla.org)

About System.Net: that is a .NET namespace, not a JavaScript library, so you cannot import it in the browser. If you need .NET functionality (for example, sending HTTP requests with server credentials), expose that logic through an ASP.NET Web API endpoint and call it from JavaScript:

// front end
async function getStatus() {
  const res = await fetch("/api/status");
  if (!res.ok) throw new Error("HTTP " + res.status);
  return res.json();
}

On the server, implement /api/status in ASP.NET Web API (or ASP.NET Core). If the API is on another origin, enable CORS on the server so the browser will allow the request. (learn.microsoft.com)

For dynamic loading, ’s idea of loading files on demand is on the right track, but modern JavaScript gives you a built-in option: dynamic import() returns a promise and avoids manual DOM injection:

document.getElementById("loadBtn").addEventListener("click", async () => {
  const {render} = await import("./widgets/chart.js");
  render();
});

Use static imports for your core code and dynamic imports for optional, heavy features. (developer.mozilla.org)

’s suggestion of splitting code into separate files still applies; with modules you also get clear dependencies and browser caching for free. (developer.mozilla.org)

Recommended Answers

All 3 Replies

It won't work like you want. Try this instead:

<script type="text/javascript" lang="Javascript" src="headerfile.js"/>
<script>
// your script
</script>

i want to declare the header file in javascript.....

please help me.....

You can't "import" a javascript file with a javascript file.

There is an import (and export) keyword in the javascript, but these are used for visibility of members.

I wrote some code to import libraries as the user required them... but its pretty rough. This should theoretically allow dynamic importing. But if you know what you want to use, then just use the <script src="xxx.js" language="javascript> tag.

<script type="text/javascript">

			function importLibFile(file){
				var e = document.createElement("script");
				e.src = file;
				e.type="text/javascript";
				document.getElementsByTagName("head")[0].insertBefore(e, document.getElementsByTagName("script")[0]);
			}

			function applicationRegister(appName, appFunction){
				document.applications[appName] = appFunction;
				document.applications[document.applications.length] = appName;
			}

			function init(){
				document.applications = new Array();
				document.libraries	  = new Array();
			}


			function setKeyListener(node, listener){
				node.onkeydown = listener;
				node.onkeyup = null;
			}

			function dropKeyListener(node){
				node.onkeydown = null;
				node.onkeyup = null;
			}

			function registerMainFunc(name, func){
				// threading would be nice here.
				function loadApplication(){
					try {
						applicationRegister(name, func);
					} catch(exception) {
						// while appfunction doesn't load
						setTimeout(function(){ registerMainFunc(name, func); }, 250);
					}
				};
			}

			function loadApplication(name, func, libFile){
				if(document.libraries && !document.libraries[libFile]){
					document.libraries[libFile] = true;
					importLibFile(libFile);
				}
				if(document.applications && !document.applications[name]){
					applicationRegister(name, func);
				} else {
					alert("web.sh: " + name + " already exists.");
				}
			}

			onload = function(){
				init();
				loadApplication("wevents", function(args, out){ __wevents_init(args, out); }, "javascript/wevents.js");
				loadApplication("clear", function(args, out){ __websh_clear(args, out); }, "javascript/webshutils.js");
				loadApplication("terminal", function(args, out){ __websh_terminal(args, out); }, "javascript/webshutils.js");
				loadApplication("yubnub", function(args, out){ __yubnub_init(args, out); }, "javascript/yubnub.js");
				loadApplication("wjsc", function(args, out){ __wjsc_main(args, out); }, "javascript/wjsc.js");
			}
		</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.