It's not your code as sites can block themselves from being framed.
This is because of a certain header set (X-Frame-Options) as per the mozilla docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<iframe src="https://www.google.com/" frameborder="0"></iframe>
</body>
</html>
But with another domain (example.com):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<iframe src="https://www.example.com/" frameborder="0"></iframe>
</body>
</html>
In cases where you aren't allowed to view the site, you can rely on the server side for this. Basically the concept is the server will fetch the file and append it at the given location.
As pointed out by @Dale's answer, this can be done with php.
For those using other server side languages, basically the same principle is applied.
An implementation of this in node will be something like:
const express = require('express');
const fs = require('fs');
const { execSync } = require('child_process');
const app = new express();
const PORT = 3000;
const fetchWebsite = (url) => {
execSync(`wget -q -O - ${url} > site.html`,
(error, stdout, stderr) => {
if (error !== null) {
return false;
}
});
}
app.get('/', async (req, res) => {
fs.writeFileSync('site.html', '', () => console.log('Created site.html'));
fs.createReadStream('site.html').pipe(res);
fetchWebsite('https://www.stackoverflow.com');
});
app.listen(PORT, () => {console.log("Listening at port: ", PORT)} )
Of course this assumes you're using a standard Linux server and you've installed the express module.