SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Function Fails in Chrome

    Code JavaScript:
                    function backspace_delete(e, delete_ele_id) 
    		{
    			if (!e) 
    			{ 
    				// IE reports window.event instead of the argument
    				e = window.event;
    			}
    			var keycode;
     
    			if (document.all) 
    			{
    				// IE
    				keycode = e.keyCode;
    			} 
    			else 
    			{
    				// Not IE
    				keycode = e.which;
    			}
     
    			if (keycode == 8 && with_field.value == '')
    			 {
    				if (document.all)
    				{
    					//IE
    					removeElementById(delete_ele_id);
    				} 
    				else 
    				{
    					//Non IE
    					removeElementById(delete_ele_id);
    				}
    			}
    		}

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I'm investigating the cause, but first a small note on the formatting of your code.

    You may be used to putting brackets on the left in some other language, but in Javascript that is broken. In Javascript you should put brackets on the right to prevent the language from misunderstand you.

    One of these works as expected, the other one doesn't.

    Code javascript:
    return {
        animal: 'dog',
        name: 'Doug'
    };


    Code javascript:
    return
    {
        animal: 'dog',
        name: 'Doug'
    };

    Try to work out why one of them doesn't work. It involves strange things like automatic-semicolon-insertion, designated code blocks, and strangely enough, labels.

    JavaScript is one of the few languages where the formatting of your braces is not just a stylistic choice, it really does make a difference to whether your code runs or not.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I've attempted to also create some HTML and scripting code to use the above function, and in the process found that with_field is a variable that hasn't been defined.

    Do you have a simple test page that demonstrates the use of your function? Because we can provide advice on how to improve what you are doing there.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    I've attempted to also create some HTML and scripting code to use the above function, and in the process found that with_field is a variable that hasn't been defined.

    Do you have a simple test page that demonstrates the use of your function? Because we can provide advice on how to improve what you are doing there.
    Paul... I'll work on formatting my javascript properly from now on. I didn't know you couldn't change the "style" and I had no clue it would make a difference in the output. Anyway... to work out this chrome issue I made a JS fiddle. This really showcases the problem. Open the fiddle in firefox, hit backspace in the input field and you'll see the div disappear. Open it in chrome and it's a lost cause. I could really use your help on this.

    Link to the JS Fiddle
    Edit this Fiddle - jsFiddle - Online Editor for the Web (JavaScript, MooTools, jQuery, Prototype, YUI, Glow and Dojo, HTML, CSS)

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The problem stems from Internet Explorer. Who would have guessed.

    IE doesn't fire the keypress event for special keys that don't change the field value. Such special keys include delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab. It seems that Webkit (used for Chrome and Safari) decided to be compatible with Internet Explorer in this regard.
    Techy details: detecting keystrokes

    The onkeydown and onkeyup events do fire for those special keys though.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The problem stems from Internet Explorer. Who would have guessed.

    IE doesn't fire the keypress event for special keys that don't change the field value. Such special keys include delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab. It seems that Webkit (used for Chrome and Safari) decided to be compatible with Internet Explorer in this regard.
    Techy details: detecting keystrokes

    The onkeydown and onkeyup events do fire for those special keys though.
    So I suppose all I have to do is change it to an onkeydown function?

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by unemployment View Post
    So I suppose all I have to do is change it to an onkeydown function?
    Try it out, what do you find?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •