This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Mar 21, 2011, 3:48 PM
1 Posts

IE Javascript Bug: Array.IndexOf

  • Category: Dojo and Client Side JavaScript
  • Platform: All
  • Release: 8.5.2
  • Role: Developer
  • Tags: IE
  • Replies: 2
This is more of a JavaScript coding question.  I have run into the problem of IE not supporting the JavaScript implementation of the Array.IndexOf function.  There is a good description of the problem and a solution here: http://stellapower.net/content/javascript-support-and-arrayindexof-ie
 
Unfortunately I have not been able to get it to work and am looking for feedback on where I am going wrong.  IE complains that prototype is undefined.  Having never used prototype before I am at a loss as to what I am missing.

I have this code (CSJS) in the Submit button of my XPage:

    var bool_repTeam = false;
    var str_thisTeam = document.getElementById("#{id:surveyTeam}").value;
    var repTeams = new Array("P1", "B1", "M1");
    var str_browser = "#{javascript:context.getUserAgent().getBrowser()}";

    if(str_browser == "IE") {
        repTeams.prototype.indexOf = function(obj){
            for(var i=0; i<this.length; i++){
                if(this[i]==obj){
                    bool_repTeam = true;
                }
            }
        }
    } else {
        if(repTeams.indexOf(str_thisTeam) > -1) {
            bool_repTeam = true;
        }
    }
 
Anyone come across this before?
Mar 21, 2011, 5:17 PM
22 Posts
Re: What you are doing does not need to do the prototype process....
You would use the prototype process to extend the Array class. but if you only use it in one place, try this instead:
 
    var bool_repTeam = false;
    var str_thisTeam = document.getElementById("#{id:surveyTeam}").value;
    var repTeams = new Array("P1", "B1", "M1");
    var str_browser = "#{javascript:context.getUserAgent().getBrowser()}";

    if(str_browser == "IE") {
       for(var i=0; i<repTeams.length; i++){
          if(repTeams[i] == str_thisTeam){
              bool_repTeam = true;
          }
       }
    } else {
        if(repTeams.indexOf(str_thisTeam) > -1) {
            bool_repTeam = true;
        }
    }
 
Newbs
Mar 21, 2011, 5:41 PM
30 Posts
Re: IE Javascript Bug: Array.IndexOf
 You need to include the code so that it's executed BEFORE indexOf is called.
 
Just add this to the top of your CSJS-library: (as per the instructions from the link) 
 
if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}
 
Copy/Paste the above to avoid any typos.
 
What prototype does is to append functionality to an already existing object, you can easily add your own (like done above). Just make sure that you always have consistent return points.
 
 
/J 

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal