Title: Using javascript in WordPress
Last modified: January 9, 2017

---

# Using javascript in WordPress

 *  [unklee](https://wordpress.org/support/users/unklee/)
 * (@unklee)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/using-javascript-in-wordpress-2/)
 * Hi,
 * A while back I migrated a whole bunch of HTML pages into pages on a WordPress
   blog. I eventually solved all the problems (often thanks to people here), but
   I find I still have one javascript problem.
 * At then end of every page I had a form which consisted of a simple opportunity
   to feedback by selecting one of three radio buttons. A javascript file responded
   when a button was pressed, called a php file and posted the page name and the
   feedback (2, 1 or 0 for good, indifferent or bad) to my MYSQL database.
 * The structure of the javascript file was simple:
 * First, a response thanking the visitor for giving feedback. Second a Try-Catch
   error trapping routine testing for new XMLHttpRequest(), Third, calling the php
   file to post to the database.
 * However running the same javascript file in WordPress gives me the initial response,
   but nothing more. I am a total javascript novice, but I presume the second section
   is no longer working. It is of the form:
 *     ```
        var request = false;
          try {
            request = new XMLHttpRequest();
          }  catch (failed) {
                request = false;
          }
   
          if (!request) {
            return;
       	}
       ```
   
 * (The original file also tested for new ActiveXObject but I think that is no longer
   required.)
 * Somehow, despite being a javascript dummy, I managed to get the original file
   working. Can anyone give me a clue, please, on what is required to make the same
   type of script work in WordPress?
 * Thanks.

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

 *  [swayam.tejwani](https://wordpress.org/support/users/swayamtejwani/)
 * (@swayamtejwani)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/using-javascript-in-wordpress-2/#post-8633110)
 * Seems like you want to call an ajax request to your PHP file which does some 
   operations in database and then return you response back, you need to see this
   resource [https://codex.wordpress.org/AJAX_in_Plugins](https://codex.wordpress.org/AJAX_in_Plugins)
   which helps you achieve same.
 *  Thread Starter [unklee](https://wordpress.org/support/users/unklee/)
 * (@unklee)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/using-javascript-in-wordpress-2/#post-8633345)
 * Thanks for replying. I’m trying to avoid totally re-writing this because I’m 
   not very competent. So I first want to see if I can revise what I have. Do you
   know if there is a reason why what I have won’t work in WordPress?
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [9 years, 5 months ago](https://wordpress.org/support/topic/using-javascript-in-wordpress-2/#post-8633885)
 * It might be easier for us to debug this by looking at the form on the page. Is
   it possible to link us to that page?
 *  Thread Starter [unklee](https://wordpress.org/support/users/unklee/)
 * (@unklee)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/using-javascript-in-wordpress-2/#post-8636147)
 * OK thanks. Here is one of many pages with the same form at the end: [http://www.is-there-a-god.info/clues/designfacts/](http://www.is-there-a-god.info/clues/designfacts/)
 * The javascript file, which is located in my theme’s footer, used to have this
   code:
 *     ```
       <script type="text/javascript">
       function hresponse(helpfulResponse) {
       	document.getElementById("feedback").innerHTML = "";
       	document.getElementById("formfeed").innerHTML = "Thank you for your feedback.";
          var request = false;
          try {
            request = new XMLHttpRequest();
          } catch (trymicrosoft) {
            try {
              request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (othermicrosoft) {
              try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (failed) {
                request = false;
              }  
            }
          }
          if (!request) {
            return;
       	}
          var url = "URL of php file";
          request.open("POST", url, true);
       	var page = document.title;
       	var helpful = 'Result='+helpfulResponse +'&Page='+page;
          request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       	request.send(helpful);
       }
       </script>
       ```
   
 * But I have since removed the two `try .... ActiveXObject` lines.
 *  Thread Starter [unklee](https://wordpress.org/support/users/unklee/)
 * (@unklee)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/using-javascript-in-wordpress-2/#post-8689227)
 * I’m wondering, hoping that maybe someone has an answer to this question please?
   Thanks.

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

The topic ‘Using javascript in WordPress’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 3 participants
 * Last reply from: [unklee](https://wordpress.org/support/users/unklee/)
 * Last activity: [9 years, 4 months ago](https://wordpress.org/support/topic/using-javascript-in-wordpress-2/#post-8689227)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
