XWidgetSoft Forum
https://www.bbs.xwidget.com/

Jscript incompatibilities in .indexOf method and arrays
https://www.bbs.xwidget.com/viewtopic.php?f=3&t=6864
Page 1 of 1

Author:  yereverluvinuncleber [ February 19th, 2018, 2:41 am ]
Post subject:  Jscript incompatibilities in .indexOf method and arrays

There are incompatibilities in .indexOf method and the handling of arrays in Microsoft JScript and ECMA javascript - these incompatibilities should be noted as they will have an impact on your javascript code.

I'm not raising this as a bug for Tony to fix it is just something to be noted.

The indexOf() method returns the position of the first occurrence of a specified value in a string.
The ECMA standard javascript allows you to use the indexOf() method on an array so you can search for the occurrence of a string in several array elements.

Code:
var validTypes = [".mpeg", ".mp4", ".mpg", ".flv"];       
if (validTypes.indexOf(xtn(filename)) === -1) {
          alert("incompatible file type");
} else {
            dprint("fileName " + fileName);
            // do something
            PlayFile(fileName);
}


Unfortunately, JScript arrays have no .IndexOf() method so validTypes cannot be an array of valid suffixes, we have to use our own home-grown validate_filetype function instead.

Code:
var validTypes = ["mpeg", "mp4", "mpg", "flv"];
if (validate_filetype(fileName,validTypes) === true ) {
            dprint("fileName " + fileName);
            // do something
            PlayFile(fileName);
} else {
          alert("incompatible file type");
}


Code:
function validate_filetype(fileName,allowed_extensions)
{   
   for(var i = 0; i < allowed_extensions.length; i++)
   {
       dprint("allowed_extensions[i] "+ allowed_extensions[i]);
       if(allowed_extensions[i]==file_extension.toLowerCase())
       {
           dprint("return true");
           return true; // valid file extension
       }
   }
               //dprint("return false");
   return false;
}

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/