1

I'm trying to import a JavaScript function into my HTML document. It work's on Chrome however in IE11 it returning 'aFunction' is undefined.

I've read numerous help topics but am unable to find a solution yet.

My Code is as follows.

Index.html

<head>...
<script  type="text/javascript" src="test.js"></script>
<script type="text/javascript">
        $(document).ready(function() {
                var myFunction = aFunction();
                //print to console to see if variable has anything in it
                console.log(myFunction);
            });
</script>
...</head>

test.js

function aFunction(){ //some code... }

Any ideas would be much appreciated.

johnnylak
  • 337
  • 3
  • 8
  • So this thread no longer needs to be responded to as I found the answer here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals . I did not think the template literals were important in the first case but now I know otherwise thank for trying to help me solve this. – johnnylak Feb 08 '18 at 01:46
  • https://stackoverflow.com/questions/40871705/template-literals-not-working-in-ie11-when-use-strict-directive-is-used – Barmar Feb 08 '18 at 02:09
  • Please delete this question if you're abandoning it. – isherwood Feb 26 '20 at 18:21

2 Answers2

1

If you want your code to work in IE11, you should transpile it with Babel.

Vais
  • 103
  • 1
  • 9
0

You made a mistake: you called aFunction instead to assign it to myFunction. You need to change your code in the following way:

var myFunction = aFunction;
//print to console to see if variable has anything in it
console.log(myFunction);
//now you can call your aFunction using myFunction var
myFunction();

UPDATE:

You get undefined because aFunction doesn't have a return: in such case, the returned value is 'undefined'. Using "var myFunction = aFunction();" you assign to myFunction the result of aFunction, that is undefined, the value you got on your console.

debe
  • 1,752
  • 2
  • 12
  • 11
  • No, he wants to call the function and print the value returned. – Barmar Feb 08 '18 at 01:44
  • @Barmar: he get undefined just because aFunction doesn't return anything. If he put a return value inside aFunction, its code already works even in IE11: he doesn't need to use Babel: you can try it. He called its var myFunction, so it seem he want to assign aFunction to myFunction var, not to execute it. – debe Feb 08 '18 at 01:51
  • His function contains a template literal, so he's getting a syntax error when he tries to define it because IE11 doesn't have template literals. That's why the function isn't defined. – Barmar Feb 08 '18 at 01:55
  • @Barmar ok, now I saw he wrote about template literals answering to his question: my answer is right for the code he wrote in the original answer. – debe Feb 08 '18 at 02:04
  • Your answer doesn't explain why he gets an error saying `aFunction is undefined`. It just tries to print something different. – Barmar Feb 08 '18 at 02:06
  • If `aFunction` is undefined, your code will still report an error when it tries to execute `var myFunction = aFunction;`. So you haven't really solved anything. – Barmar Feb 08 '18 at 02:07
  • aFunction is not undefined, it is in the test.js file. It just doesn't have a return, so it return undefined: that's why myFunction contains undefined. You can try it: its code print undefined, if you add a return to aFunction, the console will print the returned value, if you use "var myFunction = aFunction;", the console will print the function – debe Feb 08 '18 at 02:19
  • He's getting an error message saying that the function is undefined. A function that returns undefined doesn't cause an error. – Barmar Feb 08 '18 at 02:20
  • According to his bogus answer, the function in `test.js` uses a template literal. That prevents the function from being defined. – Barmar Feb 08 '18 at 02:21