robbestad / react-iframe

Simple solution for using iframes in React
ISC License
436 stars 79 forks source link

Adding capability to reference the iframe from onLoad prop function #33

Closed austintp closed 6 years ago

austintp commented 6 years ago

This minor change allows the function referenced by the onLoad prop to receive a reference to the iframe easily.

e.g.

const afterLoaded = (iframe) => {
    console.log(iframe.contentWindow.location.href);
}

class Demo extends PureComponent {
    render() {
        return (
            <Iframe
                url="http://localhost:8080/same/origin/url#hashValue=found"
                width="450px"
                height="450px"
                id="myId"
                className="myClassname"
                display="initial"
                position="relative"
                onLoad={afterLoaded}
                allowFullScreen
            />
        )
    }
}

This is useful in instances where there are redirects and the final URL that the iframe is redirected to, assuming the URL is the same domain/port/scheme as the parent. The reference to the iframe allows for other iframe manipulations as well, reading the URL was just useful for my specific use case.

austintp commented 6 years ago

Just realized this would prevent user parameters from being passed to the user specified function in onLoad prop. I'm closing this pull request as it may break how people are already using the onLoad prop.