-1

I have a page that has some functions that I would like to load when the page gets loaded .. Currently the function is called only when a button is clicked, I would also like this to loaded on page load / refresh

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';
}

1 Answers1

1

We can do that by adding an onload function to the body

<body onload ="LOADMEONSTARTUP()">

This can be achieved through Jquery where we use

$(document).ready(function(){
LOADMEONSTARTUP(); 
})

or you can refer to this post:

Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

hope i helped :)

Anil Simon
  • 121
  • 1
  • 7
  • both work but I have 20 functions to load .. so it only loads like 5 of them and then stops when page loads :( – username_copied Mar 02 '18 at 12:38
  • Are only 5 functions loading always? Or does it vary? If so you might need to check the architecture of your project with your higher ups..if it's only 5 errors...it can be a syntax error. There is no other reason why the javascript should stop loading anytime :) – Anil Simon Mar 03 '18 at 02:45
  • only 5, it like when you browse to the site once the site loads it then stops – username_copied Mar 03 '18 at 16:31
  • it might be a syntax error which is avoiding the entire code to execute..however i cannot be sure unless you show me the entire code that is being used here. – Anil Simon Mar 05 '18 at 05:59