• ddbell

    (@ddbell)


    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;){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;){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

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    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

    (@ddbell)

    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?

    Thread Starter ddbell

    (@ddbell)

    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

    (@chubbycrow)

    Thread Starter ddbell

    (@ddbell)

    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

    (@chubbycrow)

    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/
    It allows you to create code-blocks and drop them in wherever they’re needed: pages, posts, categories, etc.

    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

    (@chubbycrow)

    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

    (@ddbell)

    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.

    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.