Title: Javascript in page body?
Last modified: August 24, 2016

---

# Javascript in page body?

 *  [ddbell](https://wordpress.org/support/users/ddbell/)
 * (@ddbell)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/)
 * I want to add some very basic javascript to the page body. WordPress is not allowing
   it to execute properly. I read the “adding javascript to wordpress” article. 
   It seems to say that the javascript needs to be in a separate JS file. Is that
   correct?
 * I just want to execute a few lines of javascript inline, not calling another 
   file.
 * Here is the code I use-
    <script type=”text/javascript”> if (window.location.
   href==”[http://mydomain.com/my-page/&#8221](http://mydomain.com/my-page/&#8221);){
   document.write(‘<h2>Test!!!!!</h2>’);} </script>
 * This is what the page source code looks like from the browser. Wordpres adds 
   a <p> tage and a 
    tage. plus the header never displays.
 * <script type=”text/javascript”>
    if (window.location.href==”[http://mydomain.com/my-page/&#8221](http://mydomain.com/my-page/&#8221);){
   document.write(‘ <h2>Test!!!!!</h2> <p>’);} </script>
 * If I change the documnet.write to this then plain text “Test!!!!!” appears on
   the page. Therefore, it is at least executing the script in that instance.
 * document.write(‘Test!!!!!’);
 * Why does wordpress try to outsmart me! I know HTML and javascript and I know 
   what I want in the body of the page. Is there any way just to tell wordpress 
   that all I want from it is the header.php and footer.php files and I have complete
   control of the exact coding inbetween the header and footer. I don’t want it 
   to add any extra tags and I want to be able to use javascript inside the body
   of the page without needed to create a file for it.
 * I am not very experienced in wordpress but I am experiences in HTML, Javascript,
   and PHP to some degree. Is there a way to have more control over the page code?

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085070)
 * > This is what the page source code looks like from the browser. Wordpres adds
   > a <p> tage and a
   >  tage. plus the header never displays.
 * You didn’t say where you added the JavaScript in the end
 *  Thread Starter [ddbell](https://wordpress.org/support/users/ddbell/)
 * (@ddbell)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085077)
 * I added it from the page edit screen. I use the text editor and have the “visual
   text” disabled. I’ve been adding html code to that section with no issues. However,
   the javascript does not work as it should.
 * What I really want for the majority of the pages is complete control of the exact
   code between the header and footer. I just want to enter code and not have wordpress
   do anything to the code other than just sandwich it between the header and footer
   AS IS, exactly as I enter it. Is that not possible?
 *  [Martin Robbins](https://wordpress.org/support/users/santeven/)
 * (@santeven)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085078)
 * Have you seen [http://codex.wordpress.org/Stepping_Into_Templates](http://codex.wordpress.org/Stepping_Into_Templates)?
 *  Thread Starter [ddbell](https://wordpress.org/support/users/ddbell/)
 * (@ddbell)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085089)
 * Yeah, I read that and I think I understand the basic structure of how the page
   gets mashed together. I’ve already completed redid the header and footer php 
   files so they are exactly like I want them.
 * The issue is that the code entered in the main body part of the page does not
   get treated strictly as a block of code.
 * The inline javascript does not operate as it should and extra tags get added 
   by wordpress.
 * I want to be able to just enter the raw code in and have it added “unfilter” 
   between the header and footer without wordpress modifying it in any way.
 *  [Bill](https://wordpress.org/support/users/chubbycrow/)
 * (@chubbycrow)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085101)
 * Please read this section:
    [https://codex.wordpress.org/Using_Javascript](https://codex.wordpress.org/Using_Javascript)
 *  Thread Starter [ddbell](https://wordpress.org/support/users/ddbell/)
 * (@ddbell)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085108)
 * I did read that as mentioned in my original post but the article was somtwhat
   vague. That article suggests that it is not possible to use inline javascripting
   in a page however mentions that it may be possible with a plugin. If so, what
   plugin? I tried the “Raw HTML” plugin but that did not work.
 * I don’t understand why it is so difficult just to get a simple block of code 
   to work correctly. There must be a way to get WP to allow you to use your own
   code exctly as you want, including on page JS. I don’t want to create additional
   JS files for simple 1 or 2 line JS code. WP already has enough http requests 
   and I don’t want to make more for little one-off tiny scripts.
 * This is why I typically build websites from scratch. However, I’m building this
   for a client who needs it in wordpress so I have no choice but to try and shove
   a square peg in a round hole.
 *  [Bill](https://wordpress.org/support/users/chubbycrow/)
 * (@chubbycrow)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085111)
 * I’m sorry but I missed your mention of that page in your first post. Here is 
   one potential plugin that looks like it’s popular and highly rated: [https://wordpress.org/plugins/css-javascript-toolbox/](https://wordpress.org/plugins/css-javascript-toolbox/)
   
   It allows you to create code-blocks and drop them in wherever they’re needed:
   pages, posts, categories, etc.
 *  [Martin Robbins](https://wordpress.org/support/users/santeven/)
 * (@santeven)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085119)
 * More plugins => more http requests !
 * It is possible to utilize (simple) JS inline, just not the preferred nor recommended
   WordPress method. And typically you have to write it in a template file, not 
   the “content editor”.
 * In your case, find the template that calls
    `<?php the_content(); ?>` and try
   your JS immediately after that.
 *  [Bill](https://wordpress.org/support/users/chubbycrow/)
 * (@chubbycrow)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085121)
 * I was assuming you wanted it on specific pages, but adding to a template will
   add it to every page that uses the template (just to clarify).
 *  Thread Starter [ddbell](https://wordpress.org/support/users/ddbell/)
 * (@ddbell)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085123)
 * Thanks for the info on that plugin. I’ll check that one out too.
 * I just tested “Insert HTML Snippets” by XYZ Scripts. Entering the same exact 
   JS, creating a snippet and then adding the snippet to the page works fine. I 
   suppose I could always create each page body as one big “snippet”. This would
   ensure the code was used as is. Ir seems like a clunky way to build a website
   but I guess as long as it works.
 * I guess I’m just so used to creating websites from scratch that it just bothers
   me to have to jump through so many hoops to do such basic coding. However, my
   client wants a large directory on his site so I figured using wordpress with 
   a directory plugin would be easier than creating a functional directory database
   from scratch.
 * Bascially, I’m trying to fit wordpress into the site so it can utilize the plugin
   instead of trying to fit a site into wordpress.
 *  [Martin Robbins](https://wordpress.org/support/users/santeven/)
 * (@santeven)
 * [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085124)
 * if you need the inline JS on only some of the “pages” generated by a template,
   you can try write the conditional in the JS …
 * Directory plugins (and directory themes), that’s now a whole new story.
 * Good luck!

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Javascript in page body?’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 4 participants
 * Last reply from: [Martin Robbins](https://wordpress.org/support/users/santeven/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/javascript-in-page-body/#post-6085124)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
