0

I want to display another html page on my html page, that html page is generated. So I don't have it's src, only the content. It has doctype, head, body, all stuff. So I decided to use iframe. The problem is that I can't assign the whole text to a variable, as I get errors, that there is an unsupported character, like "<" etc. How to deal with it?

I'm trying to do something like this:

window.onload = function() {
var iframeDocument = document.getElementById("myIframe").contentDocument;
var ss = "
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>

</head>
<body>
</body>
</html>";
iframeDocument.write(ss);
iframeDocument.close();
}

So my problem is how to escape that text so it won't cause browser errors? Also html content is generated dynamically, so in my code there is no that text, there is struts action variable. But after my code gots executed in browser, that's what I see in there.

4
  • 1
    stackoverflow.com/questions/8988855/… Commented Dec 13, 2013 at 14:29
  • Don't you mean ss = "...? Also, you should escape the " signs in the string. Commented Dec 13, 2013 at 14:30
  • I suppose I have to do that on my server side. Commented Dec 13, 2013 at 14:31
  • Or you could use single quotes on the outside: var ss = '<div class="some-class">Enter HTML please</div>'; Commented Dec 13, 2013 at 14:31

1 Answer 1

1

The easiest way to solve this is to create a new aspx page, that creates that html you need in your iframe and set the src of the iframe to that aspx page (or php, depends on what you are using).

So lets say you create a new aspx page called Frame.aspx. You then add <iframe src="Frame.aspx?SomeParameter=SomeValue" /> to your parent window.

This is a much nicer approach and much more maintainable. Also it prevents having the iFrame contents twice (once in your javascript variable and once as content of the iframe).

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

Comments

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.