Link to home
Start Free TrialLog in
Avatar of Anu
AnuFlag for India

asked on

Javascript with Infopath form without custom code

Hi All,

I am trying to add  Javascript or JQuery in my Infopath form. I am looking for a workaround without any custom code. How can we refer a javascript file to an Infopath form.

I want to extend the tool tip duration in my form and I tried doing with XML as a secondary data source but it doesn't read my script or css file.

Any suggestions would be appreciable. Hoping for a reply soon.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

This is discussed in this post here https://sharepoint.stackexchange.com/questions/58204/add-js-to-an-infopath-form

Which refers to the following article
http://riteshudupak.blogspot.co.za/2010/09/add-jquery-to-browser-enabled-infopath.html
From the above
1). There is a core.js file loaded when you open your browser enabled infopath form. This is our key to success. Location of the file for me is http://sitename/_layouts/inc/core.js and may differ for you guys.

 2). Get the head tag of the page.
     
   var head = document.getElementsByTagName('head')[0];

 3). Including jquery in head tag.
     
      var script1 = document.createElement('script');
   script1.type = 'text/javascript';
   script1.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js';
   head.appendChild(script1);

4). Include one more .js file in head where you can write your jquery code.
     
       var script2 = document.createElement('script');
   script2.type = 'text/javascript';
   script2.src = '/_layouts/inc/customjs.js';
   head.appendChild(script2);

5). Include .css file for styling in the head section.
      var style1 = document.createElement('link');
   style1.rel = 'stylesheet';
   style1.type = 'text/css';
   style1.href = '/_layouts/inc/CustomCss.css';
   head.appendChild(style1);

6). Verify whether your jquery document ready works by writing the following in customjs.js file
       
        $(function(){
          alert('It Works');
    });

 7). IMPORTANT - Your infopath form is loaded to browser after the document ready function is called hence your controls in infopath are still undefined.
    Solution:    
      window.onload = function() {
         window.setTimeout(readyCall, 1000);
   }
      function readyCall(){
        alert('It works');
   }

 readyCall is a function which is called after a delay of 1 second  and within that time your infopath form should be loaded else increase the delay. Now there is no need to write JQuery's document ready. Write all your code in the function you call after window.setTimeout.

8). WARNING!! - The core.js file is loaded for all your infopath forms in your site hence including customjs.js file might create serious problems. So, before including the customjs.js file, verify your url first and include the file.
            Use this in core.js file to check your URL
       if(window.location = "/*URL of infopath form*/")
       {
          //include jquery, javascript and css file.
       }

Now you can use jquery to make ajax calls to get data into your forms or add fading effects, animations etc. The use is endless. Remember, Use this only if you cannot get something to work in infopath the normal way.
Avatar of Anu

ASKER

Hi Julian,

Thanks for sharing the steps and it is explained in detail.

But it seems that with this approach, the system files are modified so any scripts/functionality written in core.js file will be called in other forms/sites as well.

In my scenario, I actually wanted the functionality to be scoped to a single form. Using js and css, want to increase the tooltip duration so that end user get this leverage to read the full message in a single hover.
From what I have read I don't think that is possible. The process I posted was the only one I could find that in anyway addressed this.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.