Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why doesnt my JavaScript code work in Firefox and Chrome but fine in IE?

Posted on 2011-09-02
14
Medium Priority
?
423 Views
Last Modified: 2012-05-12
I have a small JavaScript function which allows a user to press the arrow keys on the keyboard to navigate a html form.  It works fine in IE but it doesnt work at all in Firefox and Chrome and I cant understand why?

Many thanks


function move(cell) {
      x = event.keyCode;

      if (x > 36 && x < 41) {
        var cell = document.getElementById(cell).name; var col = cell.substring(5, 6); var row = cell.substring(2, 4);
        var tab = cell.substring(0, 1);
        switch (x) {
          case 37:
            col = parseFloat(col) - 1;
            document.getElementById(tab + "r" + row + "c" + col).focus();
            break;
          case 38:
            row = parseFloat(row) - 1; 
            row = (row < 10) ? "0" + row : row;
            document.getElementById(tab + "r" + row + "c" + col).focus(); 
            break;
          case 39:
            col = parseFloat(col) + 1;
            document.getElementById(tab + "r" + row + "c" + col).focus();
            break;
          case 40:
            row = parseFloat(row) + 1;
            row = (row < 10) ? "0" + row : row;
            document.getElementById(tab + "r" + row + "c" + col).focus(); 
            break;
        }
      }

Open in new window

0
Comment
Question by:cjohnson300
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 4

Expert Comment

by:mohammad827
ID: 36472293
Normally if this function is called on some event, then that event has to go as in parameter to function. Check using "window.event"
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36472295
In each of your case statements, you are defining either col or row but not both, and you are using both as your id in your getElementById statement.

Could this be an issue?
0
 

Author Comment

by:cjohnson300
ID: 36472302
OmniUnlimited:  All I can say it works just as I intended in IE!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36472304
Sorry, didn't see the entire top statement.
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36472309
Stupid question, but you do have javascript enabled in both browsers?
0
 

Author Comment

by:cjohnson300
ID: 36472310
Omni: Definitely, all sites (including this one) work fine
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 36472323
When you have this on the screen in Firefox, press Ctl-Shift-J to bring up the error console and Firefox will probably tell you why it's not working.  Chrome has a similar feature.
0
 

Author Comment

by:cjohnson300
ID: 36472344
DaveBaldwin:  Thanks for that, very useful.  I'm getting "event not defined" on the line on the second line?
0
 
LVL 4

Expert Comment

by:mohammad827
ID: 36472357
Did you try window.event?
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 36472360
Look here: http://forums.asp.net/t/1190120.aspx  Firefox and IE don't always do things the same way.
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36472396
You need to check the keycode for  Netscape/Firefox/Opera like

if(window.event) // IE
{
  iKeyCode = objEvent.keyCode
}
else if(objEvent.which) // Netscape/Firefox/Opera
{
  iKeyCode = objEvent.which
}
 alert(iKeyCode);


Details are here http://forums.asp.net/t/1220574.aspx/1
0
 
LVL 29

Expert Comment

by:Badotz
ID: 36472661
How and where is function move(cell) invoked?

Traditionally, the function would be declared as
function move(e){...}

Open in new window

and the element that triggered the event and the event itself are teased out of the parameter "e" like this:
function resolve_event(e) {
    var result = { "source":null, "target":null };
    if (e) {
        result.source = e;
        
        if (result.source.target) { // FF
            result.target = result.source.target;
        }
        else { // IE7+
            result.target = result.source.srcElement;
        }
    }
    else {
        result.source = window.event; 
        result.target = result.source.srcElement;
    }
    if (result.target && result.target.nodeType === 3) { // defeat Safari bug
        result.target = result.target.parentNode;
    }
    return result;
}
//
function move(e) {
    var x = resolve_event(e); // Handle the event
    var who = x.target; // Event sink
    var ev = x.source; // Event object
    ...
    ...
}

Open in new window

0
 

Author Comment

by:cjohnson300
ID: 36472800
Brilliant, it now recognises that key press, many thanks all.

However, now it says:
document.getElementById(tab + "r" + row + "c" + col is null

could it be the double quotes?

0
 

Author Comment

by:cjohnson300
ID: 36472819
my mistake, i'd forgotten to give my form elements ids, i had just given them names

thanks to all who helped points on the way
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Several part series to implement Internet Explorer 11 Enterprise Mode
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question