Skip to content

WordPress: Add Javascript On A Page Or Post

worpress-javascript-for-one-page-or-post

In order to add your own javascript program directly on to a wordpress page or post:

First of all, you need to: add the code using the HTML view,
you can’t add this code using the Visual tab.

Create a new .js file and locate it via FTP to:

/wp-content/themes/your-theme/javascripts/your-javascript-file.js

Once at the at the Page or Post,
You will need to call the javascript file like this: (Use HTML View)

<script type="text/javascript" src="http://www.your-site.com/wp-content/themes/your-theme/javascripts/your-javascript-file.js"></script>

Here there is an example of the code that could be at the page or post:


<script type="text/javascript" src="http://www.your-site.com/wp-content/themes/your-theme/javascripts/your-javascript-file.js"></script>

<form name="theForm">
   <div class="the-form">Input Power In Kilowatts: <input class="btn_display" type="text" name="input"> 
    <span class="stylered"></span></div>
<div class="show-result"><input class="btn_calculator" type="button" value="Calculate" onClick="doMath()"><input class="btn_calculator" type="button" name="clear" value=" Clear Entry " onClick="clearAll()"></div>
    <div class="f5">
      BTU/HR Result: <input class="btn_display" type="text" name="display">
    </div>
   </form>

And this could be an example of code that you find @ the javascript file:

function doMath() { var one = eval(document.theForm.input.value); if ( (document.theForm.input.value != '' )) { var prod = one * '3412'; document.theForm.display.value=prod.toFixed(0); } else { alert ( "Form is not complete. Please Input KILOWATTS." ); } } function clearAll() { document.theForm.input.value=''; document.theForm.display.value=''; }

You can find this code working at HVACNEXT.com

Read more from WordPress

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

*