K
K
Kovalsky2020-05-02 15:35:55
JavaScript
Kovalsky, 2020-05-02 15:35:55

How to track full page load?

The initial task is simple: programmatically open the page at the specified URL and do something after that, for example, output information to the console that everything has successfully loaded. But for some absolutely incomprehensible reasons for me, onload is not triggered on some sites, although the site seems to have all the permissions. The code turned out like this:

function loadPage(url) {
    return new Promise((res, rej) => {
        let my_lovely_window;

        try {
            my_lovely_window = window.open(url)
        } catch (e) {
            rej(e);
        }

        my_lovely_window.addEventListener('load', res, true);
        my_lovely_window.addEventListener('error', rej, true);
    });
}

loadPage('/')
  .then(e => console.log(`page "${ decodeURIComponent(e.target.location.href) }" loaded successfully`))
  .catch(e => console.log(`error occurred: `, e))

Code with logs

function loadPage(url) {
    return new Promise((res, rej) => {
        let my_lovely_window;

        console.log(`starting procedure with url "${ url }"`);

        try {
            console.log('   creating window...')
            my_lovely_window = window.open(url)
            console.log('   window created!')
        } catch (e) {
            console.log('error!')
            rej(e);
        }

        console.log('   setting listeners...')
        my_lovely_window.addEventListener('load', res, true);
        my_lovely_window.addEventListener('error', rej, true);
        console.log('   listeners are set!');
    });
}

loadPage('/')
  .then(e => console.log(`page "${ decodeURIComponent(e.target.location.href) }" loaded successfully`))
  .catch(e => console.log(`error occurred: `, e))

Behavior on different sites (and platforms) is different:
If you execute this code "/" on the toaster, then an error will be displayed in the console without a message. This is probably due to the redirect,
VK does not trigger either an error or an onload 0_0
Wikipedia.org opens normally, but when you try to open a non-existent page, it behaves the same as VK - neither an error nor a load is triggered.

How to track the full loading of the page in all of the above cases? Including how to track the loading of 404 pages?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2020-05-03
@xmoonlight

You can try that .

K
Kirill Gorelov, 2020-05-03
@Kirill-Gorelov

I'm using this

window.onload = function () {//тело функции}
//или
document.addEventListener("DOMContentLoaded", function() {
   //func
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question