How can I put PHP code inside JavaScript document.write

1.1k Views Asked by At

Hello can anyone please help me in fixing this code?

1st code

<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{
    document.write('**2nd code should be here**');
}
else
{
    document.write('<img scr="./img.png" />');
}
</script>

2nd code

<?php while (have_posts()) : the_post(); get_template_part('item-video');
endwhile; ?>

What I want to happen is to insert the 2nd code into the first code. But when I try to insert it, it gives me a blank page.

1

There are 1 best solutions below

0
On

the biggest thing I've heard is that PHP is server side, while Javascript is client side. Therefore, changing the html page using document.write() isn't going to do anything unless you reload the page. I think you can circumvent this problem tho, using Ajax and the load() function.