最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I add 10 functions to a code, I don't even call any of them, but the code stops working! - Stack Overflow

programmeradmin1浏览0评论

Alone, this code works:

CustomButton = {

1: function () {
  alert("Just testing") 
  },

}

I add the code below and the code above stops working:

function getvisitingnow() {
    return document.location;
}
function getcontents(uri) {
    var req = new XMLHttpRequest();
    req.open('GET', uri, true);
    req.onreadystatechange = function (aEvt) {
        if (req.readyState == 4) {
            if(req.status == 200) {
                return req.responseText;
            }
        }
    };
    req.send();
}
function regexforsitefound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var sitefound = searchcontents.match(re);
    if (sitefound[0]) return sitefound[0] else return null;
}
function regexforcategoryfound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var categoryfound = searchcontents.match(re);
    if (categoryfound[1]) return categoryfound[1] else return null;
}
function regexfordomainname(uri) {
    var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
    var domainname = uri.match(re);
    if (domainname) return domainname;
}
function regexforparentdir(uri) {
    var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
    var parentdir = uri.match(re);
    if (parentdir) return parentdir;
}
function getparisonlink(visitingnow) {
    var searchuri = null;
    var searchcontents = null;
    var uri = visitingnow;
    while(true) {
        searchuri = '?';
        searchuri += 'q='+ uri +'&btnG=Search+Directory&hl=en&cat=gwd%2FTop';
        searchcontents = getcontents(searchuri);
        var sitefound = regexforsitefound(searchcontents);
        if (sitefound) {
            var categoryfound = regexforcategoryfound(searchcontents);
            if (categoryfound) {
                return categoryfound;
            }
        } else {
            var domainname = regexfordomainname(uri);
            if (!domainname) {
                var parentdir = regexforparentdir(uri);
                uri = parentdir;
            } else {
                return null;
            }
        }
    }
}
function clickedlink(event){
    var visitingnow = getvisitingnow(); 
    if (visitingnow) {
        getparisonlink(visitingnow);
        if (parisonlink) {
            tab.open(parisonlink);
        };
    }
}
function createBookmarkItem() {
    const XUL_NS = ".is.only.xul";
    var item = document.createElementNS(XUL_NS, "toolbarbutton");
    item.setAttribute("id", "Testing-Doit-Button2");
    item.setAttribute("class", "bookmark-item pagerank");
    item.setAttribute("tooltiptext", "Do it!");
    item.setAttribute("onmand", "testing_doit();");
    return item;
}
function placeBookmarkItem() {
    var toolbar = document.getElementById("PersonalToolbar");
    var button = createBookmarkItem();
    toolbar.appendChild(button);
}

Why?

Alone, this code works:

CustomButton = {

1: function () {
  alert("Just testing") 
  },

}

I add the code below and the code above stops working:

function getvisitingnow() {
    return document.location;
}
function getcontents(uri) {
    var req = new XMLHttpRequest();
    req.open('GET', uri, true);
    req.onreadystatechange = function (aEvt) {
        if (req.readyState == 4) {
            if(req.status == 200) {
                return req.responseText;
            }
        }
    };
    req.send();
}
function regexforsitefound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var sitefound = searchcontents.match(re);
    if (sitefound[0]) return sitefound[0] else return null;
}
function regexforcategoryfound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var categoryfound = searchcontents.match(re);
    if (categoryfound[1]) return categoryfound[1] else return null;
}
function regexfordomainname(uri) {
    var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
    var domainname = uri.match(re);
    if (domainname) return domainname;
}
function regexforparentdir(uri) {
    var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
    var parentdir = uri.match(re);
    if (parentdir) return parentdir;
}
function getparisonlink(visitingnow) {
    var searchuri = null;
    var searchcontents = null;
    var uri = visitingnow;
    while(true) {
        searchuri = 'http://www.google..br/search?';
        searchuri += 'q='+ uri +'&btnG=Search+Directory&hl=en&cat=gwd%2FTop';
        searchcontents = getcontents(searchuri);
        var sitefound = regexforsitefound(searchcontents);
        if (sitefound) {
            var categoryfound = regexforcategoryfound(searchcontents);
            if (categoryfound) {
                return categoryfound;
            }
        } else {
            var domainname = regexfordomainname(uri);
            if (!domainname) {
                var parentdir = regexforparentdir(uri);
                uri = parentdir;
            } else {
                return null;
            }
        }
    }
}
function clickedlink(event){
    var visitingnow = getvisitingnow(); 
    if (visitingnow) {
        getparisonlink(visitingnow);
        if (parisonlink) {
            tab.open(parisonlink);
        };
    }
}
function createBookmarkItem() {
    const XUL_NS = "http://www.mozilla/keymaster/gatekeeper/there.is.only.xul";
    var item = document.createElementNS(XUL_NS, "toolbarbutton");
    item.setAttribute("id", "Testing-Doit-Button2");
    item.setAttribute("class", "bookmark-item pagerank");
    item.setAttribute("tooltiptext", "Do it!");
    item.setAttribute("onmand", "testing_doit();");
    return item;
}
function placeBookmarkItem() {
    var toolbar = document.getElementById("PersonalToolbar");
    var button = createBookmarkItem();
    toolbar.appendChild(button);
}

Why?

Share Improve this question edited Nov 19, 2009 at 15:07 Delirium tremens asked Nov 19, 2009 at 14:38 Delirium tremensDelirium tremens 4,7499 gold badges47 silver badges59 bronze badges 3
  • 2 Maybe there are errors in the code that you have inserted. Check The Firefox Error Log to see if something breaks. – Bobby Commented Nov 19, 2009 at 14:40
  • 1 Have you tried just adding one function at a time? If there are syntax errors anywhere in the page, then all Javascript will cease to function. – Noah Goodrich Commented Nov 19, 2009 at 14:45
  • I have found it helpful to call toJavaScriptConsole() right when your extension loads (just put it at the top of your js file, or in an init() method if you have one). If you don't see the Error Console show up, it's probably because of a syntax error. Just remember to remove that call before you package up and distribute your extension – Tyler Commented Jun 18, 2010 at 20:58
Add a ment  | 

5 Answers 5

Reset to default 9

try adding your functions one by one. see at which function your code stops working. then empty the function contents only to put it back with pieces at a time. check again where your code stops working. about there should be a syntax error.

But as Bobby suggests, the easier way is to try Firefox Errorlog, or maybe Firebug.

One little JavaScript-error can break a lot of things. You have forgotten to add semicolons in two places.

There needs to be a semicolon after sitefound[0] here:

function regexforsitefound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?    ){2}\\</div\\>", "mi");
    var sitefound = searchcontents.match(re);
    if (sitefound[0]) return sitefound[0] else return null; 
}

and one after categoryfound[1] here:

function regexforcategoryfound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var categoryfound = searchcontents.match(re);
    if (categoryfound[1]) return categoryfound[1] else return null;
}
if (sitefound[0]) return sitefound[0] else return null;

This syntax is invalid.

Try:

if (sitefound[0])
    return sitefound[0];
else 
    return null;

If you are a Mac user, open (a recent version) of Safari and hit

⌥⌘ + i,

which opens up a great panel with lots of charts and data about the client-server interaction. You can also see and locate javascript errors, or debug javascript in a console directly. neat.

For Firefox, try the excellent firebug to see, what went wrong where .. in their own words: [with Firebug] .. you can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.

The ma after the function in CustomButton can break code the code in IE. Also, if you are using CustomButton the first time here, you should introduce it with var. I know these are not the issues you asked for, but otherwise, everything seems correct.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论