11

I have the following code that requests the Google.com homepage and sends the page data back to an Iframe on the client side.

 var options = {
    host: 'www.google.com',
    port: 80,
    path: '/',
    method: 'GET'
  };

  var req = http.get(options, function(res) {
    var pageData = "";
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      pageData += chunk;
    });

    res.on('end', function(){
      response.send(pageData)
    });
  });

However, all images and CSS are broken in the iframe? How can I preserve the images and CSS?

2 Answers 2

11

Simplest solution is to add <base href="http://google.com/"> to the html. Preferably in the head, so do string replace on '<head>' and replace in with '<head><base href="http://google.com/">'

Sign up to request clarification or add additional context in comments.

4 Comments

PS. you probably also have to strip the JS, because AJAX won't work cross origin.
Hm, I've never seen that before.... Sounds reasonable, I'm going to give it a shot, thank you ; )
Dude, you saved me another week of wanting to bang my head against my monitor. Thank you, a thousand times thank you.
You're a thousand times welcome. I think your monitor will be pleased :-)
3

Wouldn't it be easier to have the client fetch the Google page?

<html>
<head>
<script>
window.onload = function () {
  var nif = document.createElement("iframe");
  nif.width = 850;
  nif.height = 500;
  nif.src = "http://www.google.de";
  nif.appendChild( document.createTextNode("no iframe support") );
  document.body.appendChild(nif);
};
</script>
</head>
<body>
<h1>IFRAME</h1>
</body>
</html>

4 Comments

Ahh, sorry, I failed to mention that I'm on the server side making the request via Node js.
Sure, I noticed; I just didn't understand why when it is easier on the client side. Well, there may be a reason ... just couldn't figure out what it could be.
Actually, I now see how your solution is viable. I'm going to give this a shot also.
Good. :) And of course you can hardcode the IFRAME, making it even easier - if that's allowed by the problem's constraints.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.