5

I have a TypeScript file named listeners.ts that defines event listeners like this:

export function TimeField_OnChange () {
  // ...
}

There is an index.html in which I want to use this function like this (which doesn't work):

document.getElementById("timeFieldCheckBox").addEventListener("change", TimeField_OnChange);

The browsers JavaScript engine cannot find the function. How do I reference it correctly?

6
  • 1
    When you use export all variables are scoped inside IIFE to prevent them bleeding to global scope. So they will not be available on HTML page. You can create a global variable and assign this variable to it to access on HTML but you should not do it. Ideally this operation should be a part of some component and it should be there. Commented Aug 1, 2017 at 10:08
  • @Rajesh: So what would be a reasonable solution in your opinion? Commented Aug 1, 2017 at 10:11
  • Ideally, this should be a part of component and you should do this operation there. If I assume correct, there is a field that accepts time and you wish to have a checkbox that enable/disable this input field on selection of this field. or something like this. Commented Aug 1, 2017 at 10:11
  • By component you mean the HTML file? The thing is that I also need the function in other TypeScript files and I don't want to repeat the code. Commented Aug 1, 2017 at 10:13
  • When you say you are using TS, I assumed, you are using React/Angular. If yes, you will have a Component/Directive. If not, you can just create a global variable and use it from there as it will be faster Commented Aug 1, 2017 at 10:15

1 Answer 1

2

This example shows how you can add event listeners to dom elements, without having any code in index.html. The listener and the handler are inside two different modules.

utils.ts

export function TimeField_OnChange (e:Event) {
   // your event handler here
}

main.ts

import TimeField_OnChange from "./utils";

export function initPage () {
    // here you add listeners to dom elements
    let box = document.getElementById("timeFieldCheckBox");
    box.addEventListener("change", (e:Event) => TimeField_OnChange(e));
  }
}
// this is the starting point of your app
initPage();

bundling

Now you have to bundle your *.ts files into one main.js file using webpack. You need webpack because otherwise import and export won't work.

You can also bundle just using tsc but then you have to leave out import and export. It will still work, and you can still have your handler in a different .ts file. The only difference with webpack is that now your TimeField_OnChange and initPage functions will be in the global scope.

index.html

The html file only contains a link to the main.js file.

<script src="js/main.js"></script>
Sign up to request clarification or add additional context in comments.

6 Comments

But I would like to use the function in other TypeScript modules too.
Then you have to import the module before you can use it, see edit.
But if I put the function into a module, I can no longer reference it in my HTML file, can I?
If you have a <script> tag in your HTML that isn't part of your module build process, then you COULD make your function available to the global window by using: window.TimeField_OnChange = .... Then you can call it from anywhere you want. Of course, the whole idea of modules is that you don't pollute the global scope. It would be better not to have any javascript in your HTML file. Why do you need that?
Thank you for another suggestion. The purpose is not to duplicate code. The function is used as an event listener, but also somewhere else when the controls are initialized. I solved it by attaching the event listener in the main.ts file (not in any function, but right at the beginning of the file), which is the first one included into index.html by the module loader (require.js).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.