php with embedded javascript outputs newlines on HTML tags?

I have a php page with JavaScript to be used on a wordpress page,but when the page is generated all <div> <p> etc. appends new lines. Example :

Test.php ->

Read More
<script type="text/javascript">
alert('hello<div>world</div>');
</script>

When I view-source there is new lines on each html-tags : ‘div’ – which breaks the JavaScript syntax

view-source-picture

I really don’t get it

Thanks

Related posts

Leave a Reply

1 comment

  1. This is a feature of WordPress called wpautop. It will insert <br> and <p> tags automatically, depending on the number of newlines you use in your editor. You can disable this globally by creating a new plugin with this single line of code. (Or, alternatively, put this in your themes function.php)

    <?php remove_filter ('the_content',  'wpautop');
    

    However, this might break other pages, so minifying your Javascript into a single line is probably the better alternative.