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/”){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/”){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?
The topic ‘Javascript in page body?’ is closed to new replies.