How do you create a method with parameters for a custom object in JavaScript? -
i have real problem can't solve , i'm @ computers. building object using javascript has pushed me on edge. have 3 external js files , html file initiates call...
setup 1
popup.html file code:
...
<script type="text/javascript" src="methodobjects.js"></script> <script type="text/javascript" src="popup (test objects).js"></script>
...
methodobject.js file code:
var urldisectoro={ function urldisector(url){ //here compiler says: unexpected identifier uncaught referenceerror: //i need pass url function... //test if url has google search in var myurl = "" + url; var index = mysearch(myurl, "https://www.google.com/search?q="); if(index==-1){ document.getelementbyid('currentlink').innerhtml = myurl + "<br /><br />this url <font color='blue'><b><u><i>won't</i></u></b></font> blocked!"; } else{ if(index==0){ document.getelementbyid('currentlink').innerhtml = myurl + "<br /><br />this url <font color='red'><b><u><i>will</i></u></b></font> blocked!"; } else{ return "error"; } } } function mysearch(str, str1){ //searches url 2 strings passed in... var index=-1; var strlength=str.length; var str1length=str1.length; var length=0; if(strlength>str1length){ length=str1length; } else length=strlength; for(var i=0; i<length; i++){ if(str.charat(i)==str1.charat(i)){ if(i>index && index==-1 && i<1){ index=i; } for(var j=i; j<length-1; j++){ if(str.charat(j+1)==str1.charat(j+1)){ i=j; break; } else{ index=-1; break; } }//end of inner loop if(index==-1){ break; } } }//end of outer loop if(index>0){ return -1; } else{ return index; } }//end search method }
popup (object methods test).js file code:
//this file doing calling , passing in stuff other file above... //the html file called js file , starts , works until gets down to... var txtu=""; function geturl(){ chrome.tabs.getselected(null, function(tab) { var url=""; document.getelementbyid('currentlink').innerhtml = tab.url; url = "" + document.getelementbyid('currentlink').innerhtml; txtu+=url; alert("the url is:\n\n" + txtu); urldisectoro.urldisector(txtu); //object call here! //here doesn't work!!! //the compiler says: uncaught syntaxerror: urldisectoro not defined. }); } geturl();//the call! main method's call!
the thing bothers me is: popup.js file code works when external .js file , line of code in html file...
setup 2
html file code:
...
<script type="text/javascript" src="methodobjects.js"></script>
...
popup.js file code:
var txtu=""; function geturl(){ chrome.tabs.getselected(null, function(tab) { var url=""; document.getelementbyid('currentlink').innerhtml = tab.url; url = "" + document.getelementbyid('currentlink').innerhtml; txtu+=url; alert("the url is:\n\n" + txtu); urldisector(txtu); }); } geturl();//the call! main method's call! function urldisector(url){ var myurl = "" + url; var index = mysearch(myurl, "https://www.google.com/search?q="); if(index==-1){ document.getelementbyid('currentlink').innerhtml = myurl + "<br /><br />this url <font color='blue'><b><u><i>won't</i></u></b></font> blocked!"; } else{ if(index==0){ document.getelementbyid('currentlink').innerhtml = myurl + "<br /><br />this url <font color='red'><b><u><i>will</i></u></b></font> blocked!"; } else{ return "error"; } } } function mysearch(str, str1){ var index=-1; var strlength=str.length; var str1length=str1.length; var length=0; if(strlength>str1length){ length=str1length; } else length=strlength; for(var i=0; i<length; i++){ if(str.charat(i)==str1.charat(i)){ if(i>index && index==-1 && i<1){ index=i; } for(var j=i; j<length-1; j++){ if(str.charat(j+1)==str1.charat(j+1)){ i=j; break; } else{ index=-1; break; } }//end of inner loop if(index==-1){ break; } } }//end of outer loop if(index>0){ return -1; } else{ return index; } }//end of method
the thing is: want use objects have access these methods time instead of 1 specific case... objects make code little more flexible... calling them needed (setup 1 - popup (object methods test).js) though still want keep setup above , not have revert setup below...
any , error fix instructions appreciated!
i can't understand problem because sam said, have posted way information. however, think have included js files in wrong order inside html file. try writing :
<script type="text/javascript" src="popup.js"></script>
above other.
also, take time edit question. make shorter , more clear.
Comments
Post a Comment