Answers.Body: <p>I ran into a similar situation where my code would work in Firebug or Chrome JavaScript console but if I used cj(function(){});, the page level scripts were still loading and my code came in too early to have the page level scripts in scope. </p>
  
  <p>In my case I used the following:</p>
  
  <pre><code>cj(window).load(function(){
      // your code
  });
  </code></pre>
  
  <p>It's worth giving that a try.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-28T00:37:42.803Z
Answers.LastActivityDate: 2015-03-28T00:37:42.803Z
Answers.UserDisplayName: Andrew Wasson
Answers.Body: <p><code>cj(function ($) {})</code> has an implicit document.ready. To unpack that dense few characters of code:</p>
  
  <ul>
  <li><code>cj</code> is the deprecated alias of CiviCRM's copy of jQuery. In 4.5 and above use the new one: <code>CRM.$</code>.</li>
  <li>Passing a function straight into jQuery is an alias of calling <code>jQuery(document).ready()</code> (not one that we made up, it's a jQuery thing).</li>
  <li>The argument your function receives (<code>$</code>) is yet another alias of jQuery, which you should use inside your code block to make it more readable (so your code inside should be <code>$('.crm-button-type-upload...</code> not <code>cj(...</code>).</li>
  <li>So the whole thing, written out in full would be <code>CRM.$(document).ready(function($) {$('.crm-button-type-upload').hide();});</code> (not that I'm saying you can't use the shorter form <code>CRM.$(function($)...</code> I just wanted to make it clear what's going on)</li>
  </ul>
  
  <p>To answer your question about how to include your js. As of 4.5, <code>CRM_Core_Resources</code> <em>will</em> add javascript files to snippets, and by default it does. I just tested it out and was able to get a script file loaded when I clicked to open an inline edit form on the contact summary page.</p>
  
  <p>The only time this wouldn't work is if you specify the "header" region as the location for your script. This is because ajax snippets do not have a header.</p>
  
  <p>You can find out more about loading javascript files and ajax at: <a href="http://wiki.civicrm.org/confluence/display/CRMDOC/Resource+Reference" rel="nofollow">http://wiki.civicrm.org/confluence/display/CRMDOC/Resource+Reference</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-26T22:59:15.503Z
Answers.Text: Ah.  As you might have guessed from my use of `cj`, this is for Civi 4.4.  I guess I should have specified.  I'll save this project for a post-4.6 upgrade!
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-26T22:48:45.570Z
Answers.LastActivityDate: 2015-03-26T22:48:45.570Z
Answers.UserDisplayName: Coleman
Body: <p>Normally, if I want to inject a Javascript snippet into a form, I use a hook like in an extension like this:
  <code>
  function alternateduplicatecheck_civicrm_buildForm($formName, &amp;$form) {
    if($formName == 'CRM_Contact_Form_Inline_CustomData'){
      CRM_Core_Resources::singleton()-&gt;addScriptFile( 'org.cpehn.alternateduplicatecheck', 'alternate_duplicate_button.js');
      }
    }
  </code>
  This works on most forms; however, this doesn't appear to work for forms used for inline editing loaded via AJAX.  The hook is called, but the Javascript doesn't run.  Maybe I need to modify my Javascript, insert an onLoad in there?  It looks like:</p>
  
  <p><code>
  cj(function ($) {
    'use strict';
    console.log('hi');
    cj('.crm-button-type-upload').hide();
  });
  </code></p>
  
  <p>Here's the AJAX request (it's a custom field set on the summary tab): <code>http://&lt;sitename&gt;/civicrm/ajax/inline?cid=5652&amp;class_name=CRM_Contact_Form_Inline_CustomData&amp;groupID=10&amp;customRecId=339&amp;cgcount=1&amp;snippet=6&amp;reset=1</code></p>
  
Tags: <extension><hooks><ajax>
Title: How to insert Javascript into an inline form edit via an extension?
ThreadText: # How to insert Javascript into an inline form edit via an extension?
  
  (Asked by: username_0 on 2015-03-26)
  
  Normally, if I want to inject a Javascript snippet into a form, I use a hook like in an extension like this:
  `function alternateduplicatecheck_civicrm_buildForm($formName, &$form) {
  if($formName == 'CRM_Contact_Form_Inline_CustomData'){
  CRM_Core_Resources::singleton()->addScriptFile( 'org.cpehn.alternateduplicatecheck', 'alternate_duplicate_button.js');
  }
  }`
  This works on most forms; however, this doesn't appear to work for forms used for inline editing loaded via AJAX. The hook is called, but the Javascript doesn't run. Maybe I need to modify my Javascript, insert an onLoad in there? It looks like:
  
  `cj(function ($) {
  'use strict';
  console.log('hi');
  cj('.crm-button-type-upload').hide();
  });`
  
  Here's the AJAX request (it's a custom field set on the summary tab): `http://<sitename>/civicrm/ajax/inline?cid=5652&class_name=CRM_Contact_Form_Inline_CustomData&groupID=10&customRecId=339&cgcount=1&snippet=6&reset=1`
  
  **Tags:** extension, hooks, ajax
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-03-26. Score: 4 | Accepted answer
  
  `cj(function ($) {})` has an implicit document.ready. To unpack that dense few characters of code:
  
  * `cj` is the deprecated alias of CiviCRM's copy of jQuery. In 4.5 and above use the new one: `CRM.$`.
  * Passing a function straight into jQuery is an alias of calling `jQuery(document).ready()` (not one that we made up, it's a jQuery thing).
  * The argument your function receives (`$`) is yet another alias of jQuery, which you should use inside your code block to make it more readable (so your code inside should be `$('.crm-button-type-upload...` not `cj(...`).
  * So the whole thing, written out in full would be `CRM.$(document).ready(function($) {$('.crm-button-type-upload').hide();});` (not that I'm saying you can't use the shorter form `CRM.$(function($)...` I just wanted to make it clear what's going on)
  
  To answer your question about how to include your js. As of 4.5, `CRM_Core_Resources` *will* add javascript files to snippets, and by default it does. I just tested it out and was able to get a script file loaded when I clicked to open an inline edit form on the contact summary page.
  
  The only time this wouldn't work is if you specify the "header" region as the location for your script. This is because ajax snippets do not have a header.
  
  You can find out more about loading javascript files and ajax at: <http://wiki.civicrm.org/confluence/display/CRMDOC/Resource+Reference>
  
  > **Comments:**
  > **username_0**: Ah.  As you might have guessed from my use of `cj`, this is for Civi 4.4.  I guess I should have specified.  I'll save this project for a post-4.6 upgrade!  
  
  ---
  
  ### Answer by username_1 on 2015-03-28. Score: 1
  
  I ran into a similar situation where my code would work in Firebug or Chrome JavaScript console but if I used cj(function(){});, the page level scripts were still loading and my code came in too early to have the page level scripts in scope.
  
  In my case I used the following:
  
  ```
  cj(window).load(function(){
      // your code
  });
  
  ```
  
  It's worth giving that a try.
  
  
  ---
  
  