How to insert JavaScript into a PHP-Fusion article

In this article I’m going to discuss the process of adding a bit of JavaScript code into a PHP-Fusion article.

This process is going to take advantage of the built-in add_to_head output handling function that is available in PHP-Fusion. There are also other output handling functions like add_to_footer and add_to_meta that allow you to perform similar actions on your content. You can see the full list of PHP-Fusion functions like this that are built-in on the PHP-Fusion 8 Development & Project site.

Adding JavaScript to a PHP-Fusion article

Using the steps below, I’ll walk you through the process of getting a simple JavaScript alert pop-up to function within one of your PHP-Fusion articles.

  1. Login to the PHP-Fusion admin panel.
  2. click on articles

    Click on Articles.

  3. type in article content click on save article

    I have gone ahead and filled in the Subject, Snippet, and the Article content with the following text:

  4. SubjectJavaScript Hello!
    SnippetMake JavaScript say Hello!
    ArticleIn PHP-Fusion we can add JavaScript to provide extra functionality to the site!

    <p style=”display:none;”>
    add_to_head(‘<script type=”text/javascript”>alert(“Hello world!”);</script>’);

    You should notice that around the JavaScript code, I have added a <p style=”display: none;”> style to the paragraph tag to completely hide the JavaScipt code. Without this, the JavaScript code would still run on the page, however you’d still see the text add_to_head(”) displayed in the article.

    Now when we access the article that we added this JavaScript code to, we successfully see that the JavaScript code is run in our web-browser, and a pop-up message is displayed with our text.

    javascript pop up displayed

You should now know how you can get JavaScript code successfully added to one of your PHP-Fusion articles.

Stormy Scott
Stormy Scott Content Marketing Writer

Stormy is a Content Marketing Writer at InMotion Hosting. Her content focus is WordPress, web design, and help articles for small businesses.

More Articles by Stormy

Was this article helpful? Let us know!