我有一个我无法解决的真正问题,而且我非常擅长计算机。使用 javascript 构建一个对象已经把我推到了边缘。我有三个外部 js 文件和启动调用的 HTML 文件......
设置 1
popup.HTML 文件代码:
...
<script type="text/javascript" src="MethodObjects.js"></script>
<script type="text/javascript" src="popup (test Objects).js"></script>
...
MethodObject.js 文件代码:
var URLDisectorO={
function URLDisector(URL){
//HERE THE COMPILER SAYS: Unexpected identifier Uncaught ReferenceError:
//I need to pass a URL into this from another function...
//test if the URL has google search in it
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> be 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> be blocked!";
}
else{
return "ERROR";
}
}
}
function mySearch(str, str1){
//Searches the URL for the two 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 for loop
if(index==-1){
break;
}
}
}//end of outer for loop
if(index>0){
return -1;
}
else{
return index;
}
}//end my search method
}
弹出(对象方法测试).js 文件代码:
//This is the file doing the calling and passing in stuff to the other file above...
//The HTML file called the JS file and it starts and it works until it 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 THIS DOESN'T WORK!!!
//The compiler says: Uncaught SyntaxError: URLDisectorO is not defined.
});
}
getURL();//The Call! The main method's call!
困扰我的是:popup.js 文件代码在它是唯一的外部 .js 文件时有效,并且这行代码在 HTML 文件中......
设置 2
HTML 文件代码:
...
<script type="text/javascript" src="MethodObjects.js"></script>
...
popup.js 文件代码:
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! The 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> be 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> be 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 for loop
if(index==-1){
break;
}
}
}//end of outer for loop
if(index>0){
return -1;
}
else{
return index;
}
}//end of method
问题是:我想使用对象,这样我就可以一直访问这些方法,而不仅仅是一个特定的情况......对象会使这段代码更加灵活......在需要的地方调用它们(SETUP 1 - popup (对象方法测试).js)虽然我仍然想保留上面的设置,而不必恢复到下面的设置......
任何帮助和错误修复说明将不胜感激!