Solved

php with embedded javascript outputs newlines on HTML tags?

Posted on 2013-11-30
6
400 Views
Last Modified: 2013-12-01
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 ->

<script type="text/javascript">
alert('hello<div>world</div>');
</script>
When I view-source there is new lines on each html-tags : <div><p> - which breaks the JavaScript syntax

I tried to remove filter wpautop on content, but no go :(

Thanks
php-newlines.PNG
0
Comment
Question by:Eidos_IT
6 Comments
 
LVL 15

Expert Comment

by:eemit
ID: 39687676
You should use Element.html like this:
<div id="my-pageid">
<div id="id-to-insert-html">
</div>

- - -

<script>
var el = document.getElementById('id-to-insert-html');
el.html = '<div><p>hello world</p></div>';
</script>
0
 
LVL 2

Expert Comment

by:goubun
ID: 39687782
I think that the problem is because you are inserting the code in visual editor mode and you have to insert that only in text mode, You can change the mode of the editor in the right-top tab in the entry editor.
0
 

Author Comment

by:Eidos_IT
ID: 39687805
The issue is not the code it self, the alert stuff is just an example and not relevant.

The reason is that I am using a plugin for wordpress that just dont work, so I found out that the javascript is broken due to the line breaks and <p>.

I can't see this as an editor issue, the line break is happening in the middle of a string, and I am using clean nano on linux.

This is clearly a wordpress issue/feature, because if I run the page as a normal php page on apache2 there is no issues.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 13

Accepted Solution

by:
duncanb7 earned 500 total points
ID: 39687969
take a look at this , and just try it on link-1 replace img tag by your tag or *  in preg_replace for similar issue as yours

link-1 http://css-tricks.com/snippets/wordpress/remove-paragraph-tags-from-around-images/

linke-2 take a look if have time
http://sww.co.nz/solution-to-wordpress-adding-br-and-p-tags-around-shortcodes/

Hope it will give your some hint

Duncan
0
 

Author Comment

by:Eidos_IT
ID: 39688752
Duncan this is clearly in the right direction - THX
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39688766
Thanks for your pt

Hava a nice day

Duncan
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
Find out what you should include to make the best professional email signature for your organization.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question