homepage Welcome to WebmasterWorld Guest from 54.205.197.66
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Browsers / Google Chrome Browser
Forum Library, Charter, Moderator: open

Google Chrome Browser Forum

    
javascript function broken in chrome
javascript function broken in chrome
drooh




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

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

 

Fotiman




msg:4536733
 2:00 am on Jan 17, 2013 (gmt 0)

Define "no longer works". What happens? Do you not see the confirm dialog? Do you see the dialog but not get redirected?

lucy24




msg:4536765
 5:12 am on Jan 17, 2013 (gmt 0)

Instead it makes the text "remove" disappear


Does the "confirm" preliminary behave normally?

drooh




msg:4536869
 4:43 pm on Jan 17, 2013 (gmt 0)

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?

Fotiman




msg:4536876
 5:20 pm on Jan 17, 2013 (gmt 0)

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.

drooh




msg:4536893
 5:54 pm on Jan 17, 2013 (gmt 0)

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"?

Fotiman




msg:4536898
 6:01 pm on Jan 17, 2013 (gmt 0)

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...

drooh




msg:4536929
 7:38 pm on Jan 17, 2013 (gmt 0)

ok, did it, thanks

lucy24




msg:4536970
 10:56 pm on Jan 17, 2013 (gmt 0)

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...

drooh




msg:4537001
 12:29 am on Jan 18, 2013 (gmt 0)

changing what's inside the a tag makes no difference

SolutionFactor




msg:4537489
 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]

SolutionFactor




msg:4537491
 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?

Fotiman




msg:4537493
 8:04 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.

SolutionFactor




msg:4537501
 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.

lucy24




msg:4537517
 10:01 pm on Jan 19, 2013 (gmt 0)

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 ::

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Browsers / Google Chrome Browser
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved