cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Javascript: how start function after complete load of form?

Hi,

 

I have written some script in the settings of a form to select a lookup value in a list based on part of the url.

 

End of url looks like this:

"... %3DE1211321&RootFolder="

 

I'm taking the "E1211321" out of the url and put that as default value in a lookup, in which "E1211321" is one of the items.

 

Here's the script:

 

function referenceinput(){

var url = window.location.href;

var urlLength = url.length;

referenceFromUrl = url.substring(urlLength-20,urlLength-12);

reference = NWF$('#' + varReference +  ' option:contains(' + referenceFromUrl +')');

referenceValue = reference.val();

reference .attr('selected','selected');

NWF$('#' + varReference).parent().find('input').val(referenceValue);

};

 

varReference represents the dropdown (lookup)

 

This code works perfectly well when I call it from a button click. The dropdown indeed selects "E1211321".

 

However I want this function to happen when the user opens the form and all data have been loaded.

 

When I try the following:

 

NWF.FormFiller.Events.RegisterAfterReady(function () {

referenceinput()

});

 

or

 

NWF$(document).ready(function() {

referenceinput()

});

 

...the function starts well but since the lookup hasn't been loaded yet, nothing is selected.

 

How can I make sure the form is loaded completely before starting the function?

Labels: (2)
Tags (1)
Reply
11 Replies
Workflow Hero

Re: Javascript: how start function after complete load of form?

Hi,

You used the right way to register your script.

[Edit] : I'd better read slowly... when I've seen that the code works on a button click, my comment is wrong.

Then I made a real test of your script and it works fine for me, registered with the RegisterAfterReady function.

Could you add the line below just after the variable assignation? Do it contains what you wish?

console.log("From URL: "+ referenceFromUrl);

I think your problem is the way you select the option.

Based on this jQuery documentation : http://api.jquery.com/prop/

Could you try to use ?

reference.prop("selected", true);

0 Kudos
Accept as Solution Reply
Not applicable

Re: Javascript: how start function after complete load of form?

Yes indeed, Pierre, the console shows the correct value E1211321:

From URL: E1211321

And the dropdown shows "Please select a value..."

It is strange that it works for you and not for me...

0 Kudos
Accept as Solution Reply
Workflow Hero

Re: Javascript: how start function after complete load of form?

How many items does the lookup list contain?

I made my test with a very small list but it may be different with more items.

0 Kudos
Accept as Solution Reply
Not applicable

Re: Javascript: how start function after complete load of form?

Hi

Try this. It work for me.

ExecuteOrDelayUntilScriptLoaded(referenceinput, "sp.js");

Other way, you can use form variables to execute your functions.

Example:

Fix.png

Form variables will execute this script after form loaded .

If you do success, mark answer is correct

0 Kudos
Accept as Solution Reply
Not applicable

Re: Javascript: how start function after complete load of form?

Hoang, though both of your suggestions seem logic, none of them however works...

As for the first option: should I plainly put  ExecuteOrDelayUntilScriptLoaded(referenceinput, "sp.js");  in the settings or should it be inside the

NWF$(document).ready function?

The second option with the variable I thought is the tip of the day and cannot go wrong, still it does. Strangely with a button on the form referring to the same function, it does work. I don't understand why a button works and a recalculation doesn't...

I tried in 2 browsers, IE and GC, no difference.

Pierre: my (test) list currently contains about 50 items, but it will contain hundreds in real use.

In most situations the value should be as in the url (so it could be just a text field instead of a dropdown). In some situations though user should pick from the list.

I very much appreciate your help.

0 Kudos
Accept as Solution Reply
Not applicable

Re: Javascript: how start function after complete load of form?

Found out that it should be SP.SOD.....

(see here: https://msdn.microsoft.com/en-us/library/office/ff411788%28v=office.14%29.aspx )

My current code is:

NWF$(document).ready(function() {

SP.SOD.executeOrDelayUntilScriptLoaded(referenceinput,"sp.js");

};

function referenceinput(){

...

};

Still I get an error:

27-10-2015 9-50-00.png

Is "sp.js" the correct file name?

0 Kudos
Accept as Solution Reply
Not applicable

Re: Javascript: how start function after complete load of form?

Hi

0 Kudos
Accept as Solution Reply
Not applicable

Re: Javascript: how start function after complete load of form?

I have exported the form, Hoang, but I don't see a way to share it on this forum. Can you guide me?

Of course I could copy/paste the xml code here, but that's way too long.

0 Kudos
Accept as Solution Reply
Workflow Hero

Re: Javascript: how start function after complete load of form?

A closing parenthesis is missing to close the ready() function.

NWF$(document).ready(function() {

SP.SOD.executeOrDelayUntilScriptLoaded(referenceinput,"sp.js");

});

function referenceinput(){

...

};

Is it better with that one?

0 Kudos
Accept as Solution Reply