Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts

    avoiding eval w/ E4X?

    i am using the following E4X code to search directories on a server.
    the problem is that only firefox supports E4X at the moment.

    E4X uses a special operator: ".." to find descendants of nodes without regard to their parent.

    trouble is, that ".." raises a syntax error in other browsers, not be a valid statement as far as they are concered.

    i would like to keep the E4X code in the same file as other functions, my main module, used by all browsers.

    i have it coded as below, and this works ok i guess:

    Code:
    function harvestE4X(ina){ //e4x version
    qaa={};
    eval( " qaa = {   name:ina..displayname.toString(), \
    		path:  ina..href.toString(),  \
    		size: (ina..getcontentlength.toString().toInt() || 0),	\
    		date: (parseDate(ina..getlastmodified.toString()))	}   ")//end eval hide
    return qaa;
    }//end e4x  harvest
    
    
    
    function harvestDOM(ina){ //DOM version
    var tname=(obValsl( a.getElementsByTagName("displayname"))[0][TEXT].toString() || "untitled" );
    var tpath=(obValsl( a.getElementsByTagName("href"))[0][TEXT].toString() || "nopath" );
    var tsize=(obValsl( a.getElementsByTagName("getcontentlength"))[0][TEXT].toString() || 0 );
    var tdate=(obValsl( a.getElementsByTagName("getlastmodified"))[0][TEXT].toString() || now() );
    
     var qaa={ 	name:  tname,
    		path: tpath,
    		size: tsize,
    		date: tdate	 }
    
      return qaa;
    }//end harvest DOM
    
    
    if (!! window.XML ){ harvest=harvestE4X } else { harvest=harvestDOM }
    question:
    Is there anyway around having to eval the firefox code to hide it from other browsers? (i don't like to use eval)
    Last edited by rnd me; 12-14-2007 at 02:17 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    I'm not sure I understand. Why not skip E4X, and just use the DOM? I believe DOM methods work with XML, but I might be wrong.

    Also, does understanding obValsl() make any difference for us spectators?

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by Trinithis View Post
    does understanding obValsl() make any difference for us spectators?
    obValsl returns an array of the values in the collection passed to it.

    DOM methods do work fine.
    I think E4X is faster and simpler to code.
    this is not the only E4X code like this i have.
    In other functions, i use it to do a lot more complicated things than the code above.

    i just need a way to safely conceal it from non-E4X browsers.
    Last edited by rnd me; 12-14-2007 at 03:00 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    I see. Just one more question: What does obValsl stand for? IMO a weird name . Just as another way to do the same thing, this is what I've done in the past for converting element collections. (Not that it is necessarily better though.)

    Code:
    //for loser IE
    try {
      Array.prototype.slice.call(document.childNodes, 0, 0);
    }
    catch(e) {
      Array.prototype.slice = function(i, n) {
        if(n === undefined || n < 0)
          n = (n || 0) + this.length;
        for(var r = []; i < n; ++i)
          r.push(this[i]);
        return r;
      };
    }
    
    //example use
    Array.prototype.slice.call(document.getElementsByTagName("div"), 0);
    I've been trying to do what you want, but so far to no avail. If you can bear it, conditionally load the script based on XML support, but I got the impression that you didn't want that. I'll play with it a little longer and try to find another solution, and if I do, I'll post it.

    As a side note, if you could get around to it, could you look at:
    IF statement with detecting undefined functions
    Just a follow-up on the Array.prototype.reduce thing you mentioned with variable names.



    EDIT:
    I read your edit in that linked thread and saw this:
    Quote Originally Posted by rnd me View Post
    this doesn't seem to be nearly as big a problem as i thought.
    i should examine the diff between apply and call more in depth.
    for some reason, this code seems to be better than my earlier replacements.
    From experience, knowing how to use call and apply is one of THE key things to learn to use to code better Javascript. Others include first-class functions, closures, and prototypes. But I think you already have a solid understanding of those.

    Also, in harvestE4X, you're qaa does not have the var keyword. Bad news.
    Last edited by Trinithis; 12-14-2007 at 05:30 AM.

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    I give up. There probably isn't a way to make IE ignore the .. unless it is in a string or a regex.

    Here's what I wrote
    Code:
    var harvest = window.XML
      ? eval( 'function(el) { \
          return { \
            name: el..displayname.toString(), \
            path: el..href.toString() || "nopath", \
            size: el..getcontentlength.toString() - 0 || 0, \
            date: parseDate(el..getlastmodified.toString()) || now() \
          }; \
        } ')
      : function(el) {
          var f = harvest.getDescendantValue;
          return {
            name: f(el, "displayname") || "untitled",
            path: f(el, "href") || "nopath",
            size: f(el, "getcontentlength") - 0 || 0,
            date: parseDate(f(el, "getlastmodified")) || now()
          };
        };
    
    if(!window.XML) {
      harvest.getDescendantValue = function(el, s) {
        return el.getElementsByTagName(s)[0][TEXT].toString();
      };
    }
    But my best advise is to probably scrap the limited .. support; you're going to have to code the DOM version anyway. Plus less code === less bugs and maintenance.
    Last edited by Trinithis; 12-14-2007 at 06:05 PM. Reason: placed the eval OUTSIDE of the function
    Trinithis


  •  

    Posting Permissions

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