Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iframe/window "navigated" event #5725

Open
jakearchibald opened this issue Jul 10, 2020 · 9 comments
Open

Add iframe/window "navigated" event #5725

jakearchibald opened this issue Jul 10, 2020 · 9 comments
Labels
addition/proposal New features or enhancements topic: navigation

Comments

@jakearchibald
Copy link
Contributor

iframe.src = url;

Right now, it's difficult to know when the iframe commits its navigation. There's the 'load' event, but that waits on inner resources to load, so it can be long after the navigation is committed.

I think this timing information is already available, as you can poll iframe.contentDocument until it changes or starts throwing. At which point you know the navigation has commited.

A callback for this would solve #2161, as you could add a DOMContentLoaded listener at this point if the iframe is same-origin.


Suggested API:

iframe.addEventListener('navigated', () => {
  // Fired when a navigation commits.
});

await iframe.navigated;
// Resolves when the iframe's navigation commits.
// This promise changes each time src is set.

It's unclear if 'navigated' should fire on a failed navigation, and if .navigated should reject, due to #125, #1230, and #4247 (h/t @domenic).

I'm not sure how you'd do the same thing for WindowProxy. Would we be able to hang a .navigated promise off WindowProxy?

@domenic
Copy link
Member

domenic commented Jul 10, 2020

I'm not sure how you'd do the same thing for WindowProxy. Would we be able to hang a .navigated promise off WindowProxy?

Why would we want to do this for WindowProxy?

@jakearchibald
Copy link
Contributor Author

If we come up with a solution for iframes, it feels like we should also solve it for window.open windows, which have the same limitations.

@annevk
Copy link
Member

annevk commented Jul 13, 2020

Hmm, but once it starts throwing you wouldn't know if the user navigated further to another origin until you get another load event, right? I think we have to be quite careful here, especially since the I don't think the navigation algorithm is implemented as-is across the board.

@annevk annevk added addition/proposal New features or enhancements topic: navigation labels Jul 13, 2020
@jakearchibald
Copy link
Contributor Author

That's fair. Maybe this makes the promise more attractive, where the promise only changes when the src is set – it doesn't change when navigations are triggered within the iframe itself.

I'm not sure if this would work with WindowProxy.

@annevk
Copy link
Member

annevk commented Jul 13, 2020

As a signal for when initial about:blank has been replaced? That could work on Window I suppose as that's the only case where its associated document is changed. (Again though, there's a lot of non-interop in that area around events.)

@jakearchibald
Copy link
Contributor Author

Yeah, I suppose it could apply only to the very first navigation that happens to an iframe/window. For subsequent navigations it might make more sense to have something like windowProxy.location.navigate which returns a promise.

@Jamesernator
Copy link

Jamesernator commented Mar 4, 2021

Would this also allowing cancelling the navigation, or would this fire just after the navigation has started fetching?

For me I've been recently wanting to be able to intercept and capture navigations to insert a "Are your sure you want to go to blah?". e.g. Being able to do this would be great:

iframe.addEventListener("navigated", () => {
  if (!isSafe(iframe.contentWindow.location.href)) {
    const confirmNavigationURL = new URL("/confirmNavigation", location.href);
    confirmNavigationURL.searchParams.set("url", iframe.contentWindow.location.href);
    iframe.contentWindow.location.href = confirmNavigationURL.href;
  }
});

@domenic
Copy link
Member

domenic commented Mar 4, 2021

@Jamesernator I think your particular use case is better served by https://github.com/WICG/app-history. Although that's for your own window, not for an iframe, so maybe not...

@Jamesernator
Copy link

@Jamesernator I think your particular use case is better served by https://github.com/WICG/app-history. Although that's for your own window, not for an iframe, so maybe not...

Actually yes the cancellation behaviour of that proposal is fine for my case as I can inject scripts into the iframe, outside control would be slightly less awkward, but admittedly my use case is super super niche and appHistory would be more than sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements topic: navigation
Development

No branches or pull requests

4 participants