10 examples of 'javascript wait for page to load' in JavaScript

Every line of 'javascript wait for page to load' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
139async wait(pageCompleteCheck) {
140 const waitTime = this.options.pageCompleteWaitTime || 5000;
141 if (!pageCompleteCheck) {
142 pageCompleteCheck = this.options.pageCompleteCheckInactivity
143 ? pageCompleteCheckByInactivity
144 : defaultPageCompleteCheck;
145 // if using SPA just override
146 if (this.options.spa) {
147 pageCompleteCheck = spaCheck;
148 }
149 }
150
151 const driver = this.driver,
152 pageCompleteCheckTimeout = this.options.timeouts.pageCompleteCheck;
153 try {
154 const pageCompleteCheckCondition = new Condition(
155 'for page complete check script to return true',
156 function(d) {
157 return d.executeScript(pageCompleteCheck, waitTime).then(function(t) {
158 return t === true;
159 });
160 }
161 );
162 log.debug(
163 `Waiting for script pageCompleteCheck at most ${pageCompleteCheckTimeout} ms`
164 );
165 log.verbose(`Waiting for script ${pageCompleteCheck}`);
166 await timeout(
167 driver.wait(
168 pageCompleteCheckCondition,
169 pageCompleteCheckTimeout,
170 undefined,
171 this.options.pageCompleteCheckPollTimeout || 200
172 ),
173 pageCompleteCheckTimeout,
174 `Running page complete check ${pageCompleteCheck} took too long `
175 );
176 log.debug(`Waiting after load event for ${waitTime} ms.`);
177 await delay(waitTime);
178 } catch (e) {
179 log.error('Failed to wait ' + e);
180 throw new UrlLoadError('Failed to wait ', {
181 cause: e
182 });
183 }
184}
79waitForDocumentFullyLoaded() {
80 driver.waitUntil(
81 () => {
82 const webview = driver.execute(() => ({ state: document.readyState, title: document.title }));
83
84 return webview.state === DOCUMENT_READY_STATE.COMPLETE && webview.title !== '';
85 },
86 15000,
87 'Website not loaded',
88 100,
89 );
90}
64async function loadPage (driver, url) {
65 await retryInterval(5, 1000, async function () {
66 await openPage(driver, url);
67 const title = await spinTitle(driver);
68 title.should.not.include('Cannot Open Page');
69 });
70}
40async waitUntilLocated(element, Twaiting) {
41 let counter = Twaiting;
42 if (counter === undefined) counter = 180;
43 try {
44 do {
45 await this.driver.sleep(300);
46 if (await this.isElementLocated(element)) return true;
47 } while (counter-- > 0);
48 return false;
49 }
50 catch (err) {
51 return false;
52 }
53}
23function navigate_to_javascript_onload(test, iframe) {
24 iframe.addEventListener("load", test.step_func(e => {
25 assert_equals(typeof SecurityPolicyViolationEvent, "function");
26 iframe.contentDocument.addEventListener(
27 "securitypolicyviolation",
28 test.unreached_func("The CSP event should be fired in the embedding document, not in the embedee.")
29 );
30
31 iframe.src = "javascript:'Fail.'";
32 }));
33}
15wait(elem) {
16 browser.waitForExist(elem);
17}
10public async waitForFrameLoad(timeout?: number) {
11 return new Promise((resolve, reject) => {
12 if (!timeout) {
13 timeout = 60 * 1000; // 1 min for default timeout;
14 }
15 const contentLoaded = () => {
16 if (timeoutTimer) {
17 clearTimeout(timeoutTimer);
18 }
19 resolve();
20 };
21
22 const checkFrameInterval = setInterval(() => {
23 if (this.checkFrameState()) {
24 clearInterval(checkFrameInterval);
25 contentLoaded();
26 }
27 }, 10);
28
29 const timeoutTimer = setTimeout(() => {
30 clearInterval(checkFrameInterval);
31 reject(
32 new Error(`Timeout error. The frame did not load after ${timeout} ms`)
33 );
34 }, timeout);
35 });
36}
12async loadElement() {
13 if (this._loadRetryCount > 6) {
14 throw new Error('Load Element error');
15 }
16 await this._targetPage.waitForSelector('iframe#rc-widget-adapter-frame');
17 await this._targetPage.waitFor(200);
18 const iframes = await this._targetPage.frames();
19 this._widgetIframe = iframes.find(f => f.name() === 'rc-widget-adapter-frame');
20 if (!this._widgetIframe) {
21 this._loadRetryCount += 1;
22 await this.loadElement();
23 }
24 this._loadRetryCount = 0;
25}
14Cypress.Commands.add('waitForIFrame', function waitForIFrame() {
15 // eslint-disable-next-line
16 cy.wait(4000)
17})
18function waitUntilLoaded (window, timeout) {
19 var onStopLoading;
20 return Promise.fromNode(function (callback) {
21 onStopLoading = function () {
22 callback(null, window);
23 };
24 window.webContents.on('did-stop-loading', onStopLoading);
25
26 })
27 .timeout(timeout || 60000)
28 .finally(function () {
29 // clean up so we can reuse the window
30 window.webContents.removeListener('did-stop-loading', onStopLoading);
31 })
32 .catch(Promise.TimeoutError, function (err) {
33 window.webContents.stop();
34 throw err;
35 });
36}

Related snippets