jquery - Javascript string comparison not working, searched a lot? -
this code (it's bookmarklet)
javascript:(function(){ a=document.createelement('script'); a.setattribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); document.body.appendchild(a); data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","ranjan","text"],["#txtmother","neelam","text"],["#txtpincode","452010","text"],["#txtphone","2147483647","text"],["#txtemail","aayush@mail.com","text"]]'; for(a=$.parsejson(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] == 'text') { console.log(c); $(c[0]).val(c[1]); } } })();
it used work fine until inserted if
statement, broke. console doesn't give me errors , have googled lot javascript string comparison errors , found nothing useful.
i tried use equals
, compareto
, ended console errors , nothing working.
uncaught typeerror: cannot call method 'equals' of undefined fillform.php:1 uncaught typeerror: cannot call method 'compareto' of undefined
help highly appreciated.
note: variables named reason, being compiled google closure compiler , if
statement being edited in.
there several things wrong code; string comparison not 1 of them.
1) aren't waiting asynchronously loaded script complete. code should pretty fail because $.parsejson()
isn't available. in fact, once fixed problem, code works fine me:
(function(){ a=document.createelement('script'); a.setattribute('src','//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); var afterjqueryload = function() { data='[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","ranjan","text"],["#txtmother","neelam","text"],["#txtpincode","452010","text"],["#txtphone","2147483647","text"],["#txtemail","aayush@mail.com","text"]]'; for(a=$.parsejson(data),b=a.length-1;0<=b;b--){ c=a[b]; if (c[2] == 'text') { console.log(c); $(c[0]).val(c[1]); } } }; var jqueryready = false; a.onreadystatechange= function () { if((this.readystate == 'complete' || this.readystate == 'loaded') && !jqueryready) { jqueryready = true; afterjqueryload(); } }; a.onload = function() { if(!jqueryready) { jqueryready = true; afterjqueryload(); } }; document.body.appendchild(a); })();
2) use better var names (a, b, , c not var names).
3) use var
scope vars correctly. right code shadowing globals , stomping on vars within same scope; a
var, example, stomp on script elem var. (you should still change var names per (2) using var
not optional; must scope vars correctly.)
4) use spaces readability; for
line unnecessarily difficult read having no spaces.
all now:
(function(){ var jqueryscriptelem = document.createelement('script'); jqueryscriptelem.setattribute('src', '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'); var afterjqueryload = function() { var data = '[["#txtapplicantlname","agrawal","text"],["#txtapplicantfname","aayush","text"],["#txtfather","ranjan","text"],["#txtmother","neelam","text"],["#txtpincode","452010","text"],["#txtphone","2147483647","text"],["#txtemail","aayush@mail.com","text"]]', dataparsed = $.parsejson(data); for(var dataitemindex = dataparsed.length - 1; 0 <= dataitemindex; dataitemindex--) { var dataitem = dataparsed[dataitemindex]; if (dataitem[2] == 'text') { console.log(dataitem); $(dataitem[0]).val(dataitem[1]); } } }; var jqueryready = false; jqueryscriptelem.onreadystatechange = function () { if((this.readystate == 'complete' || this.readystate == 'loaded') && !jqueryready) { jqueryready = true; afterjqueryload(); } }; jqueryscriptelem.onload = function() { if(!jqueryready) { jqueryready = true; afterjqueryload(); } }; document.body.appendchild(jqueryscriptelem); })();
Comments
Post a Comment