简体   繁体   English

从外部JavaScript运行两个onLoad?

[英]Running Two onLoad From an External JavaScript?

I currently have two external scripts for my site that both require the use of onLoad. 我的网站目前有两个外部脚本,都需要使用onLoad。 They are: 他们是:

One that automatically generates a table of contents: 自动生成目录的表:

window.onload = function () {
var toc = "";
var level = 1;

document.getElementById("contents").innerHTML =
    document.getElementById("contents").innerHTML.replace(
        /<h([\d])>([^<]+)<\/h([\d])>/gi,
        function (str, openLevel, titleText, closeLevel) {
            if (openLevel != closeLevel) {
                return str;
            }

            if (openLevel > level) {
                toc += (new Array(openLevel - level + 1)).join("<ol>");
            } else if (openLevel < level) {
                toc += (new Array(level - openLevel + 1)).join("</ol>");
            }

            level = parseInt(openLevel);

            var anchor = titleText.replace(/ /g, "_");
            toc += "<li><a href=\"#" + anchor + "\">" + titleText
                + "</a></li>";

            return "<h" + openLevel + "><a name=\"" + anchor + "\">"
                + titleText + "</a></h" + closeLevel + ">";
        }
    );

if (level) {
    toc += (new Array(level + 1)).join("</ol>");
}

document.getElementById("toc").innerHTML += toc;
};

And another that is used to find certain words in a paragraph and replace them with given JavaScript. 另一个用于在段落中查找某些单词并将其替换为给定的JavaScript。

var doc, bod, E, makeLink;
var pre = onload;
onload = function(){
if(pre)pre();

doc = document; bod = doc.body;
E = function(id) {
  return doc.getElementById(id);
}
T = function(tag) {
  return doc.getElementsByTagName(tag);
}
makeLink = function(node, word, href) {
  if(node.innerHTML) {
    node.innerHTML = node.innerHTML.replace(word, "<a href='"+href+"'>"+word+'</a>');
  }
  return false;
}

makeLink(E('testId'), 'Within', 'within.html');
makeLink(E('testId'), 'assuming', 'assuming.html');

}

However, as they're both using onLoad, they don't work together. 但是,由于它们都使用onLoad,因此它们不能一起工作。 Is there a way to get them both to function on the same page? 有没有办法让它们在同一页面上都起作用?

Use addEventListener instead of setting the property of the window object. 使用addEventListener而不是设置window对象的属性。 Here's a MDN page . 这是MDN页面

You can do something like that: 您可以执行以下操作:

window.addEventListener('load', function() {
    console.log('Loaded 1');
});

window.addEventListener('load', function() {
    console.log('Loaded 2');
});

Both of them should fire off. 他们两个都应该开火。

functionA() {
    var toc = "";
    var level = 1;

    document.getElementById("contents").innerHTML =
        document.getElementById("contents").innerHTML.replace(
            /<h([\d])>([^<]+)<\/h([\d])>/gi,
            function (str, openLevel, titleText, closeLevel) {
                if (openLevel != closeLevel) {
                    return str;
                }

                if (openLevel > level) {
                    toc += (new Array(openLevel - level + 1)).join("<ol>");
                } else if (openLevel < level) {
                    toc += (new Array(level - openLevel + 1)).join("</ol>");
                }

                level = parseInt(openLevel);

                var anchor = titleText.replace(/ /g, "_");
                toc += "<li><a href=\"#" + anchor + "\">" + titleText
                    + "</a></li>";

                return "<h" + openLevel + "><a name=\"" + anchor + "\">"
                    + titleText + "</a></h" + closeLevel + ">";
            }
        );

    if (level) {
        toc += (new Array(level + 1)).join("</ol>");
    }

    document.getElementById("toc").innerHTML += toc;
}

functionB() {
    var doc, bod;
    //var pre = onload;
    //onload = function(){
    //if(pre)pre();

    doc = document; bod = doc.body;

    makeLink(E('testId'), 'Within', 'within.html');
    makeLink(E('testId'), 'assuming', 'assuming.html');

    }
}

window.E = function(id) {
  return doc.getElementById(id);
}
window.T = function(tag) {
  return doc.getElementsByTagName(tag);
}
window.makeLink = function(node, word, href) {
  if(node.innerHTML) {
    node.innerHTML = node.innerHTML.replace(word, "<a href='"+href+"'>"+word+'</a>');
  }
  return false;
}

Then you call onload from body tag(If you want one to execute before the other, change the order): 然后从body标签调用onload(如果您希望一个在另一个之前执行,请更改顺序):

<body onload="functionA(); functionB();">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM