开发者

How do I call a function with in a function?

I need to add i3c to my and statements. I don't know the correct syntax. This code is broken. Would an anonymous function be better? So if c0() passes, c4() runs, if c4() passes I need i3c() to run or what is in i3c to 开发者_运维技巧run.

function o1(a,b)
  {
  document.getElementById(a).value=b;
  }
function i3()
  {
  var a=document.forms['f3'].elements,b='f3e';
  c0(a,'Please enter both a tile and a url',b)&&c4(a[2],'Please enter a valid url',b)&&d0()&&s0('pi3a.php',is('f3'),s2);
  o1(f3aa,'');o1(f3bb,'');
  }
function d0()
  {
  var a=document.getElementById('Bb1c'),
  b=document.createElement('a'),
  c=document.forms['f3'].elements;  
  b.href=c[2].value;
  b.name="a1";    
  b.className ="b";
  b.innerHTML = c[1].value; 
  a.appendChild(b);
  return 1;
  }


The && (logical AND operator) is processed left to right. If any expression returns false, that value is returned. If all but the last one return true, then the value of the last expression is returned (whether it is true or false).

So if i3c is not being called, it is because the return value of one of the preceding calls is falsey, i.e. it type converts to false, so it might be 0, '' (empty string), undefined, NaN, null, ... I've probably left one out.

Edit

As patrick w commented, the line:

  c.setAttribute("class","b");

will fail in IE. Don't use setAttribute for standard attributes, use DOM properties instead:

c.href = d[2].value;   
c.name = "a1";   
c.className = "b";

Faster and less bugy.


function i3() {
    var e = 'f3e';
    if ( !(c0(a,'Please enter both a tile and a url',e)) ) { return flase; }
    if ( !(c4(a[2],'Please enter a valid url',b)) ) { return flase; }
    (function() {
        var doc = document,
            a = doc.forms.f3.elements,
            b = doc.getElementById('Bb1c'),
            c = doc.createElement('a'),
            d = doc.forms.f3.elements;
        c.href = d[2].value;
        c.name = "a1";
        c.className = "b";
        c.innerHTML = d[1].value;
        b.appendChild(c);
        // which a you need here ???
        //var a = is('f3');
        var someOtherA = is('f3');
        //------------------------
        s0('pi3a.php', a, s2);
        // if these are part of the form you use in this function
        // use 
        // document.forms[<name>].elements[<name>].value
        doc.getElementById('f3aa').value = '';
        doc.getElementById('f3bb').value = '';
    })();
}

This may work, I suppose c0 and c4 return true/false. This way they are easier to read. If they return true, continue with the next one and don`t return.

In the i3c function you declare a twice!

Look at the comments for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜