-5

Since a few updates of FireFox en Chrome, there seems to be a function I have that isn't working anymore (in JavaScript). The function's name is autocomplete, for example:

function autocomplete() {
    alert("autocomplete!");
}

Apparently the browsers don't like this function name anymore, as the function call is being ignored. I tried to rewrite this to the following:

var autocomplete = function() {
    alert("autocomplete!");
};

But that won't help me. Changing the name would, though. But I don't want to change the name everywhere where this is being used. Does any of you have a solution maybe?

Edit: The use is demonstrated in the following fiddle: http://jsfiddle.net/P43xs/ I'm trying to trigger the function by event.

user229044
  • 232,980
  • 40
  • 330
  • 338
LaVomit
  • 492
  • 5
  • 15
  • 3
    http://jsfiddle.net/UwG4e/ --- my chrome doesn't agree with you. – zerkms Jun 24 '14 at 11:41
  • It even works in IE... – Dieterg Jun 24 '14 at 11:42
  • 1
    Unable to reproduce the problem. Is there an error on the JavaScript console? Something else wrong with the code? When you debug it, where/how does it fail? There's nothing wrong with the name of your function, something else must be wrong. – David Jun 24 '14 at 11:43
  • Do you have elements in your HTML that have `autocomplete` as name? Can you create a Fiddle to reproduce the problem. – putvande Jun 24 '14 at 11:44
  • 1
    @putvande: what would it change? http://jsfiddle.net/UwG4e/1/ – zerkms Jun 24 '14 at 11:45
  • I thought that used to be a problem. Might have confused it with something else. – putvande Jun 24 '14 at 11:46
  • 2
    Hint: When you assume that all of your code must be correct and that the compiler/interpreter must be broken, you're usually wrong. – David Jun 24 '14 at 11:46
  • http://jsfiddle.net/P43xs/ Try out this fiddle. In this setting, I have the problem. – LaVomit Jun 24 '14 at 11:48
  • @David, I don't get any errors and the function doesn't get executed. It fails at the call in the event. If I try to execute another function after it, like onclick="autocomplete(); anotherFunction();", the second function will not be executed. – LaVomit Jun 24 '14 at 11:56
  • This is a valid question, should not be downvoted. I was able to reproduce the problem in Firefox 27.0 – kcak11 Jun 24 '14 at 11:57
  • It was only downvoted because the original unedited question didn't contain all of the necessary information. – Andy Jun 24 '14 at 12:00
  • +1 I could reproduce it in Chrome 35. Interestingly enough, when I add `eval("console.log(autocomplete.toString())");` in the fiddle, I still get the source for the function defined manually. I used `console.log(autocomplete.toString())` before that but switched to `eval` to make sure the second function can't keep a reference. – Aaron Digulla Jun 24 '14 at 12:00
  • Here's a similar question: http://stackoverflow.com/questions/7852237/cant-use-download-as-a-function-name-in-javascript – Andy Jun 24 '14 at 12:05

2 Answers2

2

SOLUTION:

onclick="autocomplete();"  // --> does not work

onclick="window.autocomplete();" // --> works

So, when invoking the autocomplete function, just scope it to window, and then it works and shows the alert (Tried in your fiddle)

REASON:

The input elements have an attribute by name "autocomplete" so when you mention onclick="autocomplete();", internally it evaluates to autocomplete===this.autocomplete and hence does nothing. ('this' in current context is the input element).

So, when you scope it to window.autocomplete(), then it points to the autocomplete function that you have defined.

kcak11
  • 832
  • 7
  • 19
  • My function is being displayed in the console, though it doesn't get executed through an event. – LaVomit Jun 24 '14 at 11:51
0

I can reproduce this in Chrome 35.

I've created a second fiddle with a workaround: http://jsfiddle.net/5m7UD/1/

<input type="button" onclick="autocomplete();" value="Click me!" />
<input type="button" onclick="whatever();" value="Me too please!" />
<input type="button" id="id1" value="Second attempt" />

with this script:

console.log(autocomplete)

function autocomplete() {
    alert("This doesn't work, only in IE compatible mode or other, lder browsers :(");
}

function whatever() {
    alert("This works, obviously...");
    eval("console.log(autocomplete.toString())");
}

document.getElementById('id1').onclick = autocomplete;

The last line works. It's interesting to see that the first input button doesn't work despite the fact that I see undefined in the console when I run this script and the eval prints the body of the function defined above.

(I use eval to make sure the code can't simply keep a reference to the original function).

When I run the code and click the button, I see this error in the console: Uncaught TypeError: string is not a function on the line where the button is defined (i.e. for the onclick="autocomplete();"), so it really doesn't like the HTML.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • It could be because browser is trying to relate it to the autocomplete attribute for input field. I tried using window.autocomplete(); and then everything works fine. (Strange !!) – kcak11 Jun 24 '14 at 12:23
  • Yes, window.autocomplete() or setting the onclick event by JavaScript does the trick, though it needs to be changed everywhere (different pages use the same JavaScript file). I think the best solution is to screw myself and change the name of the function everywhere. Thank you everybody for helping me out. Hope we helped others too now and in the future. – LaVomit Jun 24 '14 at 13:20
  • @LaVomit: Consider asking a question in the Chrome support group at https://productforums.google.com/forum/#!categories/chrome – Aaron Digulla Jun 24 '14 at 13:36