Inserting content into a webpage from iframe code behind in JQuery

Here’s the situation: A user clicks a button on a webpage and an iframe pops up (facebox which similar to lightbox). Now I want to inject some html from the code behind of the iframe into the parent window. I’m using JQuery to achieve this and this simple line of code does the trick:

parent.$("#MessageDiv").append("hello world");

That works fine except when you’re trying to insert a string which contains line breaks. I’m not talking about html line breaks here but windows line breaks as in new line and carriage return (\n\r). The problem is that because the string will span on several lines, the expression will be unterminated. Have a look at the following to understand what I’m trying to say more clearly:

hello world

how are you?

The above text is represent as “hello world\n\rhow are you?”. When that is written in a javascript script, it will become like this:

document.write(“hello world

how are you?);

To make this work, you need to remove the line breaks as follows:

string myContent = "hello world\n\rhow are you?";

myContent.Replace("\n", "").Replace("\r", "");

This will keep all your content on one line and make the javascript work as intended.

comments powered by Disqus