Title: Moving Javascript from inline to an external file
Last modified: August 21, 2016

---

# Moving Javascript from inline to an external file

 *  [pvijeh](https://wordpress.org/support/users/pvijeh/)
 * (@pvijeh)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/moving-javascript-from-inline-to-an-external-file/)
 * What up yall!
 * got a javascript question for all you smart people w/ sexy brains.
 * I am trying to move a bit of JS from inline (i have it inserted into a wordpress
   post) to an existing external file that I call in.
 * However– my code must require some kind of formatting changes or something- because
   it no longer works when I put it in the external file (the rest of the existing
   JS in that external file continues to work fine– however)
 * Anyone got ideas on what I need to change?
 * Here is my JS (this works inline):
 * <script type=”text/javascript”>// <![CDATA[
    document.getElementById(‘FBsharer’).
   onclick = function () { var url = ‘[https://www.facebook.com/sharer/sharer.php?u=&#8217](https://www.facebook.com/sharer/sharer.php?u=&#8217);;
   url += encodeURIComponent(location.href); window.open(url, ‘fbshare’, ‘width=
   640,height=320’); }; // ]]></script>

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

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/moving-javascript-from-inline-to-an-external-file/#post-4513236)
 * The problem is most likely to be that you’re Javascript is executing before the
   element that you’re targeting has been set up by the browser.
 * The standard way to get around this is to wrap that code in a jQuery wrapper 
   to make sure that the DOM has been set up correcdtly. As an example…
 *     ```
       jQuery (document).ready (function () {
   
           // Put your code in here.
   
       });
       ```
   
 *  Thread Starter [pvijeh](https://wordpress.org/support/users/pvijeh/)
 * (@pvijeh)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/moving-javascript-from-inline-to-an-external-file/#post-4513301)
 * thanks ill give that a shot!

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

The topic ‘Moving Javascript from inline to an external file’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [pvijeh](https://wordpress.org/support/users/pvijeh/)
 * Last activity: [12 years, 4 months ago](https://wordpress.org/support/topic/moving-javascript-from-inline-to-an-external-file/#post-4513301)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
