2

I have two scripts defined in an HTML page. I call the 'function2' script from an onclick event attached to a Submit button. When I click the submit button, I get an uncaught reference error - function1 is not defined.

function1 extracts a variable from the URL (I have in the url of the page the following variables: ?m=1&u=2&f=3).

function2 assigns (one at a time) the url variables to local variables, then assigns these local variables to be the values of 3 hidden form fields (field IDs: msg, mem, and frm).

I added the vars to show up in the console.log but they never make it there since the function isn't defined...

Any ideas what I've done wrong?

<script language="javascript">
function function1(param) {
    var pattern = new RegExp('[?&]' + param + '((=([^&]*))|(?=(&|$)))', 'i');
    var mz = window.location.search.match(pattern);
    return mz && (typeof(mz[3]) === 'undefined' ? '' : mz[3]);
}
</script>

<script language="javascript">
function function2() {  
    var mm = (function1('m'))();
    var uu = (function1('u'))();
    var ff = (function1('f'))();
    document.getElementById("msg").value=mm;
    document.getElementById("mem").value=uu;
    document.getElementById("frm").value=ff;
    console.log("mm:", mm);
    console.log("uu:", uu); 
    console.log("ff:", ff);  
    }
</script>
Whito
  • 29
  • 1
  • 6
    You've the IIFEs like: `(function1('m'))()`. At first, `function1` is executed, then JS tries to execute the returned value from `function1`. It doesn't return a function, hence an error. To fix this, just remove the IIFE, i.e `mm = function1('m');` etc. – Teemu Sep 01 '16 at 18:54
  • 2
    please show a working HTML page here, with your submit button hooked up. Although looking at this code, it feels like you learned JS from an older tutorial - modern JS doesn't need `type="javascript"` passed, and there is no point in splitting this up into two script tags. A much better thing to tell us first is: what are you trying to DO with this code, because I suspect there is far better code that can be written to achieve that. – Mike 'Pomax' Kamermans Sep 01 '16 at 18:55
  • Can you try to combine these two `script` elements into one and see if it works? – rishat Sep 01 '16 at 18:57
  • Thanks Rishat and Mike. I used your comments as well to get my code working. – Whito Sep 01 '16 at 19:17
  • It's really not that complicated. Start up your debugger--you do know how to do that, I assume. Then put a breakpoint at the `var mm` line. When you get there, try examining the value of `(function1('m'))()` (by typing it into the console). You'll get the error. Then, examine `(function1('m'))`, and you'll see that it's some value. Around this point it may dawn on you that you are trying to execute the function twice. All this is rudimentary debugging technique, which you should learn unless you plan to depend on SO to debug all your programs forever. –  Sep 01 '16 at 19:18

1 Answers1

1

You've IIFEs (Immediately Invoked Function Expression) like:

(function1('m'))();

At first, function1 is executed, then JS tries to invoke the returned value from function1, but it doesn't return a function, hence an error.

To fix this, just remove the IIFEs, i.e:

mm = function1('m');
Community
  • 1
  • 1
Teemu
  • 22,918
  • 7
  • 53
  • 106