3

I have an infopath form that I want to modify via jQuery. The only problem is that it loads after the pages DOM loads, so jQuery's standard $(document).ready(handler); won't work here. My question is is there any way to pull a $(infopath).ready(handler); of some sort, or a way to maybe wait for the infopath form to get done loading, without just using a standard setTimeout().

EDIT 1: So you have more information, the jQuery is within a Content Editor Web Part (CEWP), and the Infopath form is within an Infopath Form Viewer Web Part.

I know using jQuery or javascript with infopath isn't a standard practice, it unfortunately is a necessary one in this situation due to non-ideal functionality from infopath repeated option controls, discussed further in this question.

EDIT 2: From what I can tell, and I may very well be wrong, sharepoint has a js file called core.js, this seems to have functionality for loading, via AJAX, an infopath form into the page. Is there any way I can set a jQuery function to act after a remote javascript file finishes execution?

Community
  • 1
  • 1
Vap0r
  • 2,588
  • 3
  • 22
  • 35
  • Is the form viewer using an iframe? – James Montagne Jul 28 '11 at 18:58
  • No, I wish, I think it's using some javascript code. It's being very sneaky sharepoint back-end javascript like. (If you code any amount of javascript with sharepoint, you understand what I mean). If I can't get a decent answer here, I'll probably be left crawling through the response code for what scripts are loaded, then crawling through that code to see if there is anything I can piggy back on. – Vap0r Jul 28 '11 at 19:00
  • I feel your pain. Not with infopath specifically, but sharepoint in general. If it's javascript then you may not find an easy answer. My only thought was checking every second for a given element on the page which would indicate that it has loaded, but that's kind of ugly. – James Montagne Jul 28 '11 at 19:05
  • How is the infopath form being loaded after the DOM is loaded, specifically? Through other JS? Is it jQuery code? – barfoon Jul 28 '11 at 19:06
  • does infopath uses jQuery?? You can hack $.ajax if core.js depends on jQuery. if not could give me a link where i can see this core.js and tell you how to hack it. – Praveen Prasad Aug 10 '11 at 10:17

7 Answers7

3

There is a better way, depending on your context. My requirement is to intercept the click handler on my InfoPath form Save button and do some additional validation that is difficult to do in InfoPath. As the question states, it is not possible to use the normal jQuery $() function directly because the form isn't loaded at that point.

There is a workaround. SharePoint provides a JavaScript array _spBodyOnLoadFunctionNames and initialisation functions can be added to this array. By pushing a function onto the array, it will be called after the InfoPath form is loaded:

$(function(){
    if (window.location.pathname.match(/Item\/(new|edit)ifs\.aspx/i)){
        _spBodyOnLoadFunctionNames.push('CustomInfoPathInit');
    }
});

function CustomInfoPathInit(){
    $(document).on('focusin', 'input[value="Save"]', function(event){
        $(this).attr('onclick', null); // remove standard handler
        $(this).off('click').on('click', function(){
            alert('click');
        });
        return (Button.OnClick(this, event)); // original click handler
    });
}

The reason I'm using $(document).on('focusin', function(event){... is because any postbacks from the form (e.g. when the form displays Sending data to the server...) will result in a refresh of the form and any bound handlers will be removed. Using the jQuery $(document).on('focusin',...) method resolves this issue and using the focusin event allows the button's original click handler to be removed and the new one re-attached.

David Clarke
  • 12,888
  • 9
  • 86
  • 116
3

Try like this: In FormServer.aspx, just above </body>, put this:

<script type="text/javascript">
    _InfoPath.OnLoad2 = function() {
        _InfoPath.OnLoad();
        //Here I will put my custom javascript code, which will be invoked after loading InfoPath form
    }
    window.onload = _InfoPath.OnLoad2;
</script>

My answer also here: How to check if InfoPath form was loaded (in javascript)

2

I ended up using a timer that checks to see if a label on the InfoPath form has shown up in the DOM:

var InfoPathCheck;

$(document).ready(function(){
    InfoPathCheck = setInterval(function() {  //Wait for InfoPath to finish loading
        HasInfoPathLoaded();
    }, 200);
});

function HasInfoPathLoaded() {
    var testDiv = $('h4 span:contains("Photo")').html();
    if (testDiv) {
        clearInterval(InfoPathCheck);
        // Do stuff with the InfoPath form here....
    }
}
  • While this will work it is sub-optimal, effectively polling the page. Use the `_spBodyOnLoadFunctionNames` array to run javascript after the form is fully loaded. – David Clarke Apr 08 '14 at 20:25
0

I don't know if this is your problem or not, but if things are still loading after the $(document).ready function, try the $(window).load function. This one fires after all of your libraries, pictures, styles, and other things have finished loading.

Kyle
  • 4,421
  • 22
  • 32
0

Have you looked at XDocument.Onload Event?

According to the API docs, this occurs "after a Microsoft Office InfoPath 2007 form has been loaded, but before any views have been initialized".

Also, take a look at this post that talks about adding your onLoad handler to SharePoint's onLoad process.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • Do you know how to leverage the XDocument.Onload in javascript? – Vap0r Aug 10 '11 at 13:20
  • If you look at the [MSDN](http://msdn.microsoft.com/en-us/library/bb229737%28v=office.12%29.aspx) link (its in my post too), it has examples in Javascript (JScript in MS world). Are you looking for something more? – Mrchief Aug 10 '11 at 13:44
  • Ahh, I didn't think JScript and javascript were interchangeable. – Vap0r Aug 10 '11 at 13:49
  • They are the same (versions of ECMAScript), just different names to avoid trademark/licensing issues: http://en.wikipedia.org/wiki/JScript – Mrchief Aug 10 '11 at 14:00
  • A very generic one, the error that sharepoint gives whenever a method or operation is performed on an un-instantiated object. – Vap0r Aug 10 '11 at 20:28
  • Can you post some code? Also, how about the SP `onload` hook? Any luck with that? – Mrchief Aug 10 '11 at 20:34
  • There really isn't much code at all. And the SP onload hook won't work since the infopath form loads after that. – Vap0r Aug 10 '11 at 21:07
  • Hmm... I'll get to this tonight. – Mrchief Aug 10 '11 at 21:08
0

If finding a hook in infopath (whatever it is) code fails, you can brute force generate an event yourself. Write a function which checks for the done condition somehow using jQuery, and either triggers the event or reschedules itself to run again in 200 ms.

Then write your event handler the usual way. If you ever find a better way you just need to replace the first part.

Jürgen Strobel
  • 2,200
  • 18
  • 30
0

I have an idea but no time to try it. How about detecting XDocument.OnLoad in jscript and jscript puts a value in hidden input. And then javascript detects the event. I think it will work in IE but maybe not in the other browsers.

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240