【发布时间】:2018-08-10 15:33:57
【问题描述】:
我有一个页面,其中包含一些我想在页面加载时加载的函数。目前该函数仅在单击按钮时调用,我也希望在页面加载/刷新时加载此函数
function LOADMEONSTARTUP() {
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open('get', 'log.txt', true);
reader.onreadystatechange = function () {
if (reader.readyState == 4 && reader.status == 200) {
displayContents1(reader.responseText);
}
};
reader.send();
}
function displayContents1(content) {
var wanted = 'apples';
var words = content.split(/\s/);
var found = [];
words.forEach(function (word, index) {
if (word === wanted && words[index + 1]) {
found.push(words[index + 1]);
}
});
console.log('found:', found);
var el = document.getElementById('here1');
el.innerHTML = found.length ? found.join('<br/>') : 'nothing found';
}
【问题讨论】:
-
也许这会有所帮助? stackoverflow.com/a/9899701/815507
-
使用 window.onload = function();
标签: javascript html load