Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js iframe src

Tags:

reactjs

iframe

index.js

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component {
    render() {
        return (
            <div>
                <h1>Outside iFrame</h1>
                <iframe title="myiframe" src="./target.html" width="600px" height="400px"></iframe>
            </div>
        )
    }
}

ReactDOM.render(<App />, document.getElementById('root'));

index.html

<div id="root"></div>

target.html

<html>

<head></head>

<body>
    <h1>Inside IFrame</h1>
</body>

</html>

the Problem is like this:

enter image description here

what's wrong with the code?

like image 750
Custer Avatar asked Dec 20 '25 16:12

Custer


1 Answers

You are getting this issue because the relative url src="./target.html" is incorrect. To make sure you are using the correct url, please individually check the url generated for index.html and target.html. Also make sure that you are not making a typo error.

I tried running your code and it works fine if your target.html and index.html urls are as follows :

target.html

index.html

I can also recreate your issue, if I make a typo error in my code like so src="./typoerror.html" ,

Typo error case

like image 99
Saurabh Gour Avatar answered Dec 22 '25 10:12

Saurabh Gour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!