0

I have a onChange handler bug on my production env. I have a selectbox as follow:

<select name="select-history-one" id="select_old_version" class="form-control" onchange="showHistoryDiff();"> ... </select>

Below (inside the body tag) I have the function declared in a script tag as follow:

<script>
function showHistoryDiff(){
...
}
</script>

On my local machine in debug mode everything works just fine. As soon as I deploy it onto my webserver and try to select something I get that error message:

Uncaught ReferenceError: showHistoryDiff is not defined
at HTMLSelectElement.onchange (27:632)
onchange @ 27:632

I really have no clue why that happens.

4
  • are you sure that the JS file is present in your production version? Commented Feb 15, 2018 at 17:23
  • 1
    does the script tag in your HTML code come before the select tag? The order matters Commented Feb 15, 2018 at 17:25
  • Since the function is not in an external file, yes I am sure. Commented Feb 15, 2018 at 17:26
  • it comes after. First comes the select box and after that the script tag comes. I wonder why it works on my localmaschine just fine and on the webserver it doesn't Commented Feb 15, 2018 at 17:27

1 Answer 1

2

<body>
  <select onchange="doChange()">
   <option>A</option>
   <option>B</option>
  </select>
  
  <script>
    function doChange() {
      console.log('hi');
    }
  </script>
</body>

As you can see from this example, it should work as you described.

That likely means it isn't really an issue with the code, but more an issue with how it is loaded.

The first is to make sure that your function is actually there. How you've described it, you should be able to open up the console in Chrome and just type showHistoryDiff and it should output something like this:

ƒ showHistoryDiff() { console.log('hi'); }

If it doesn't say this, but instead says it is undefined, then for whatever reason your code isn't getting built. If that's the case, you'll want to take a very close look at your build pipeline and/or setup to make sure it's actually going where you think it should.

If it does exist, then there are a couple of possibilities: - it is getting overridden by something else and becoming undefined. Look for any other references to the function name throughout your code - there is some other JavaScript syntax error which is happening before your function is declared, so the function doesn't end up declared

One other remote possibility is if you are loading the code from two different domains, you might be running into cross-origin (CORS) issues.

Look at your console and see if there are any errors being spit out. Clearing those up should be your first goal.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It seems the that there are problems with linebreaks. As soon as I add a linebreak into the text (I want to compare text histories), the function is not known. So I have to fix that, and maybe get ride of any linebreaks befor parsing it into the html file. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.