Welcome to WebmasterWorld Guest from 184.73.126.70

Forum Moderators: open

Message Too Old, No Replies

javascript function broken in chrome

javascript function broken in chrome

   
11:46 pm on Jan 16, 2013 (gmt 0)

5+ Year Member



Any idea why this function no longer works in chrome?

<script type="text/javascript">
function remove(id){
if (confirm("Delete?")){
window.location='?current&del&id=' + id;
}
}
</script>

<a href="#" onclick="remove(273)">remove</a>


Instead it makes the text "remove" disappear
2:00 am on Jan 17, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Define "no longer works". What happens? Do you not see the confirm dialog? Do you see the dialog but not get redirected?
5:12 am on Jan 17, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Instead it makes the text "remove" disappear


Does the "confirm" preliminary behave normally?
4:43 pm on Jan 17, 2013 (gmt 0)

5+ Year Member



Ok, after doing some testing, I found that if I change the name of the function to remove1 or anything other than "remove" it works?!?

This is strange, is the word remove a reserved word in chrome or javascript declaration?
5:20 pm on Jan 17, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



It's not a JavaScript reserved word [developer.mozilla.org].

I used the Chrome Developer Tools to inspect the element.
The element is an "a" element.
It has a __proto__ of "HTMLElement", which in turn has a __proto__ of "Element", which has a method "remove".

However, I'm having a hard time finding an specifications for what an "Element" is supposed to implement, and I think this might be a browser specific object. In my opinion, you've found a valid case for concern, as "remove" doesn't seem to be documented anywhere.
5:54 pm on Jan 17, 2013 (gmt 0)

5+ Year Member



Thanks Fotiman, it used to work fine in Chrome until recently, firefox and IE are fine.

I've got this js function in about 50 cms's out there and not sure if I should update them or wait for chrome to fix this "bug"?
6:01 pm on Jan 17, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I wouldn't wait... send it to them as an issue. Click on the settings icon (formerly the wrench, now 3 horizontal lines to the right of the address bar), click on Tools > Report an issue...
7:38 pm on Jan 17, 2013 (gmt 0)

5+ Year Member



ok, did it, thanks
10:56 pm on Jan 17, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



after doing some testing, I found that if I change the name of the function to remove1 or anything other than "remove" it works?!?

What about the other way around? Anything behave differently if you change the visible content word "remove"? Back in your first post it was a detail that confused me because I had to keep checking which "remove" you (or the function) were talking about.

javascript is not the world's most complicated programming language, but it's complicated enough to have a personality. (This is probably a known corollary of Murphy's Law.) Only yesterday I had to rename a variable that was supposed to be strictly local because its content was getting changed by a function that should have had no effect on it. And I could swear I'd used the identical construction dozens of times before...
12:29 am on Jan 18, 2013 (gmt 0)

5+ Year Member



changing what's inside the a tag makes no difference
7:51 pm on Jan 19, 2013 (gmt 0)



A good way to prevent issues like this is to namespace your JavaScript code. E.g. instead of creating remove() in the global namespace like this:

<script>
function remove() {
// some code here
}

// called via remove();
</script>


You're better off creating a namespace for your app, and adding all your app's JS code to that. For example:

<script>
// create a custom namespace object for your app's js code
MyCms = {};

// add remove() to the custom namespace, not the global namespace
MyCms.remove = function() {
// some code here
};

// called via MyCms.remove();
</script>


The syntax is slightly different, but ultimately the function code will do the same.

The difference is that by stucturing your JS code that way you're forced to be explicit about which method you're calling (e.g. MyCms.remove() vs. remove()), and for good reason; that helps keep your app's code stable while browsers, other JS libraries you're using, etc., continue to change.

Edit: removed bits of my reply that previous replies already answered.

[edited by: SolutionFactor at 9:02 pm (utc) on Jan 19, 2013]

7:56 pm on Jan 19, 2013 (gmt 0)



Only yesterday I had to rename a variable that was supposed to be strictly local because its content was getting changed by a function that should have had no effect on it. And I could swear I'd used the identical construction dozens of times before...


I'd bet a pretty penny this was due to not using the var keyword before the variable declaration.

Was the variable in question declared using the var keyword, or did you leave that out?
8:04 pm on Jan 19, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



SolutionFactor, if you read the other posts in the thread, you'll see that this has already been verified and it's a case of Chrome having a "remove" method on Element objects.
8:46 pm on Jan 19, 2013 (gmt 0)



SolutionFactor, if you read the other posts in the thread, you'll see that this has already been verified and it's a case of Chrome having a "remove" method on Element objects.


Understood, I should've read better.
10:01 pm on Jan 19, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Was the variable in question declared using the var keyword, or did you leave that out?

Didn't check-- but knowing myself, you are probably spot on.

:: wandering off to compose RegEx to clean up all existing code, with particular reference to functions used on real site ::
 

Featured Threads

Hot Threads This Week

Hot Threads This Month