Asghar
Asghar

Reputation: 2346

Passing HTML to javascript function

I have a variable in which a complete html is saved. $body= $myhtmlpage;

<a onclick="openWin(' <?php echo htmlspecialchars(json_encode($body)) ?>');" href="javascript:void(0);"> Click </a>

and i have this javascript function which display the text in new window.

 <script type="text/javascript">
function openWin( str )
{
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(str+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>

When there is simple text in my body, it works fine. but if there is some html then it does not display, I am new to javascript. please tell me how can i prepare my HTML that it should be passed to Javascript html. i tried htmlspecialchars(json_encode($body)) functions but still having problem.

Uncaught SyntaxError: Unexpected identifier 

Upvotes: 1

Views: 7102

Answers (4)

davidethell
davidethell

Reputation: 12018

You will have a long battle trying to get a lot of HTML to work as a string variable in Javascript. It would be far better for you to put that markup in a hidden block (like a DIV) in your markup and then just get the contents of that markup and show it in your window.

This has the added advantage of allowing your hidden markup to be validated. It is very hard to debug a lot of html markup stuffed into a string variable, but when included in the DOM as real markup it makes your life much easier.

UPDATE: Adding some sample code:

<div id="my_hidden_content" style="display:none;">
<?php echo $body; ?>
</div>

<a onclick="openWin('my_hidden_content');" href="javascript:void(0);"> Click </a>

Now the javascript:

function openWin( contentId )
{
    var contentContainer = document.getElementById(contentId);
    var content = contentContainer.innerHTML;
    myWindow=window.open('','','width=400,height=400');
    myWindow.document.write(content+"<p>This is 'myWindow'</p>");
    myWindow.focus();
}

Upvotes: 3

DaveRandom
DaveRandom

Reputation: 88697

Firstly, you do not need to use json_encode(), that is just confusing the situation.

Secondly, the problem will be that your HTML contains quotes. This will result in a syntax error in the HTML you output, since htmlspecialchars() does not escape quotes.

Use htmlentities() with the ENT_QUOTES flag instead. So change the line to this:

<a onclick="openWin('<?php echo htmlentities($body, ENT_QUOTES) ?>');" href="javascript:void(0);">Click</a>

Thirdly (although it should probably be firstly since it is the most important point) your approach to this is all wrong. If you're opening a new window, you should have it load a page from the server and generate the HTML when the window is opened.

Upvotes: 2

iDaemon
iDaemon

Reputation: 363

You can try html code inside php code, instead php code inside html. for e.g.

<?php
echo "<a onclick='openWin(\" ".htmlspecialchars(json_encode($body))." \")'>Click</a>";
?>

Upvotes: 1

Parth Thakkar
Parth Thakkar

Reputation: 5475

That's because, HTML isn't JSON. For that simply use: htmlspecialchars($body)

Upvotes: 0

Related Questions