ajaxtoolkit progress update javascript - javascript

I am using ajaxtoolkit modalpopupextender to display a processing message. It's working fine on a typical postback (button click, etc).
However, I am calling a postback in javascript and the progress message isn't showing UNLESS I add an "alert" in the javascript.
Here's my javascript:
function getOrder() {
beginRequest('', '')
var hid = document.getElementById("ctl00_ContentPlaceHolder_hid");
//alert(hid.value);
__doPostBack("SaveOrder", hid.value);
endRequest('', '')
}
I obviously don't want to have an "alert" so does anyone have a solution or ran into this?
Here's the other pience of javascript used relating to this (typical stuff):
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
var mdlCtl = document.getElementById('ctl00_hfModalCtlID').value;
function beginRequest(sender, args){
$find(mdlCtl).show();
}
function endRequest(sender, args) {
$find(mdlCtl).hide();
}
I'm stumped on why the "alert" makes a difference in the javascript that shows the processing message...?
Thoughts? Ideas?

Related

Command Bar Menu Disappearing CRM

I am facing an issue in CRM, the bug is when I create an appointment and try to save it, the commandBar go invisible after the load of the created record of the appointment. I try to inspect the javascript associated code but there is no hint about the source of the problem, I should say that after refreshing the page of the created page the command bar become visible.
OnSave: function (e) {
EOZ.Appointment.SetPivotalId();
var eventArgs = e.getEventArgs();
if (Xrm.Page.ui.getFormType() == 1 && eventArgs.getSaveMode() == 1 && !saveAndClose) {
Xrm.Page.getAttribute("createdon").addOnChange(EOZ.Appointment.CreatedOnChanged);
}
else {
saveAndClose = true;
Xrm.Page.getAttribute("createdon").removeOnChange(EOZ.Appointment.CreatedOnChanged);
}
},
CreatedOnChanged : function () {
var id = Xrm.Page.data.entity.getId();
setTimeout(function () {
Xrm.Utility.openEntityForm("appointment", id);
}, 500);
}
NB : After a lot of javascript debugging, i noticed that the problem is only happened when the method "CreatedOnChanged" is called more than once, in the other hand if it's called just once the command bar is visible. I don't know if it's the source of the bug, but it is the only difference in the code's behaviour.
Edit :
Dynamics CRM 2013, Browser : Google chrome latest version on 03/03/2017
I modified my code to be like :
CreatedOnChanged : function () {
if(!isCalled){
var id = Xrm.Page.data.entity.getId();
setTimeout(function () {
Xrm.Utility.openEntityForm("appointment", id);
}, 500);
isCalled = true;
}
}
The method "CreatedOnChanged" is called multible times but its content is executed just once, and the command bar is displayed correctly ... But really i don't why when this method is executed multiple times, the command bar become hidden.
It look to me like you are trying to refresh the page after a save.
Rather than trying to reopen the form I think that forcing a refresh may be more correct:
Xrm.Page.data.refresh(true);
I think that you code around registering the event on the OnCreate() event is probably causing an error. The ribbon automatically refreshes after a save (so any visibility / enabling conditions or javascript can be retested.
A JavaScript error on the page would potentially cause the JavaScript re-rendering the ribbon to fail.

How do I create a wait event in dojo that waits for a user to click a button before continuing?

I have the following code in a javascript file:
if(dojo.byId('WC_selectedColorNumber') == null && this.defaultColor != null)
{
dijit.byId('WC_color_selection').domNode.style.display = 'block';
dojo.html.set(dojo.query(".message__button .add"), "Add product with only base color " + this.defaultColor + "?");
var userResponse = true;
dojo.connect(WC_add_color_yes, "onclick", function(evt){
userResponse = true;
});
dojo.connect(WC_add_color_no, "onclick", function(evt){
userResponse = false;
});
//var userResponse = confirm("Add product with only base color " + this.defaultColor + "?");
//I WANT TO WAIT HERE FOR THE RESPONSE
if(userResponse == false) //if user clicks Cancel or 'no', display a message and leave the function.
{
alert("Remember to select a color before adding to cart."); //should be a tooltip/popup (not javascript alert) with the same message
return; //return so item doesn't get added to cart
}
}
Firstly, the logic behind this code is correct and it works perfectly well when using javascript confirm's.
As of now, everything comes up and displays correctly, and clicking the buttons perform the correct actions (if I put a console.log in the onclick dojo events, they do indeed print to the console when I click the buttons). However, the program doesn't wait for the responses and continues beyond the dojo.connect methods before it sees the user's input.
I need it to wait until either the yes or no button have been pressed, but I cannot figure out how to do it. I've tried using a
while(userResponse == null);
but a) it's generally a terrible idea and b) it didn't work anyways.
How can I make my code wait until the user has clicked one of the two buttons?
If you can make a jsfiddle I'd be able to help you more, I think, but your dojo.connect calls shouldn't be inside a logic flow like this. Instead, set up your connects on widget startup, and have them act generically.
In your example code, it looks to me like saying "Yes" means "Use default color", and "No" means "User must specify color". So...
startup: function () {
this.inherited(arguments);
dojo.connect(WC_add_color_yes, "onclick", dojo.hitch(this, function(evt){
this.useDefaultColor();
}));
dojo.connect(WC_add_color_no, "onclick", dojo.hitch(this, function(evt){
this.displayColorPicker();
}));
}
And then... only display those two buttons (or the dialog they're hopefully in) when applicable.
There is no "wait" or "sleep" function in javascript and each invocation of javascript code executes to completion (it does not get interrupted in mid execution by a response to some other event). You have correcly identified the historical execeptions that overcome this - global alert and confirm functions execute in browser native code and wait on user input.
Because of this your code will have to be restructured in some way, e.g. an event handler for "add to cart" validates the color choice and calls a function to really add it to the cart if valid. If it is not valid it modifies the DOM to present user with some buttons. The handler for the "yes" option would likewise call the same function to really add it to the cart.
Specific code is outside the scope of this answer - there must be many methods in page and code design to achieve the desired result. For example only: breaking up the sequential code and putting it in separate event handlers, coding using Promise objects defined in EC6 but not supported in MSIE, or perhaps even providing an option of "none - base color only" in the color selection logic.
FYI the dojo 1.10 toolkit documentation reports support for Dojo Promises but I leave research to determine its suitability with you.

How to call JavaScript function in VBA?

I need to by pass an IE confirm 'OK'/'Cancel' pop-up message. I have a problem running a JavaScript function in my VBA script. My JavaScript:
function ConfirmSave()
{
var Ok = confirm('Are you sure all Documents and Information are attached and correct before saving?');
if(Ok)
return true;
else
return false;
}
function submitbutton_click() {
document.getElementById('FileAttachement2_hdnButtonFlag').value = "SAVE";
var submitbutton = document.getElementById('cmdDownSave');
var uploadobj=document.getElementById('FileAttachement2_Uploader1');
if(!window.filesuploaded)
{
if (!ConfirmSave()) return false;
if(uploadobj.getqueuecount()>0)
{
uploadobj.startupload();
}
else
{
//var uploadedcount=parseInt(submitbutton.getAttribute("itemcount"))||0;
//if(uploadedcount>0)
//{
return true;
//}
//alert("Please browse files for upload");
}
return false;
}
window.filesuploaded=false;
return true;
}
In manual process, when I click the save button, the page will pop-up a confirm message box, and my macro will stop running when the pop-up appears unless it has been clicked.
Here is the code I have tried, to click the save button,
Set ElementNameV = HTMLDoc.getElementsByName("cmdsave")
ElementNameV(0).click
I also tried using removeattribute and setattribute with which the pop-up message disappeared but it doesn't upload the file because I need to press the 'OK' in confirm message box that will appear upon clicking the save button to start the file uploading.
ElementNameV(0).removeAttribute ("onclick")
ElementNameV(0).setAttribute "onclick", "return true"
ElementNameV(0).click
I tried running the JavaScript function using below script but it also shows the confirm pop-up message box:
Call HTMLDoc.parentWindow.execScript("submitbutton_click()")
You should be able to overwrite the ConfirmSave function with one which simply returns true:
HTMLDoc.parentWindow.execScript "window.ConfirmSave = function(){return true;};"
or
HTMLDoc.parentWindow.execScript "window.confirm = function(){return true;};"
or even
HTMLDoc.parentWindow.eval "window.confirm = function(){return true;};"
Run that before clicking the button.
Tested and works in IE11
So I've read your question a few times now and I think that to achieve what you want to do you are going to have to completely change your approach to the problem. You need to read up on Javascript Concurency, Javascript Web Workers, and the Javascript Event Loop.
Just throw these terms into Google and you'll find lots of great resources to learn about this.
By default Javascript is a single threaded language and it halts while waiting for events to complete their activities. What you seem to be looking for based on how I'm reading your question is a way for your Javascript to keep performing actions while a user prompt is being displayed.
While this is not an endorsement, I will throw out this one link to get you started.

custom Page_ClientValidate overriden when updatepanel is used

I've got a web page that uses an UpdatePanel and validation within.
Because of requirements specified within, I have to display a custom model when validation fails and so far, I've only been able to do this by overriding the Page_ClientValidate client side function:
function Page_ClientValidate(validationGroup) {
Page_InvalidControlToBeFocused = null;
if (typeof(Page_Validators) == "undefined") {
return true;
}
var i;
for (i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators[i], validationGroup, null);
}
ValidatorUpdateIsValid();
ValidationSummaryOnSubmit(validationGroup);
Page_BlockSubmit = !Page_IsValid;
if(!Page_IsValid)
{
displayError();
}
return Page_IsValid;
}
This works great, but I've noticed that when using it within an UpdatePanel, as soon as the UpdatePanel generates new content, it seems the ScriptResource.axd's containing the original Page_ClientValidate is downloaded again, thus overriding my override (if that makes any sense).
Now, it displays the errors beside the fields, but doesn't call my displayError function.
Any suggestions?
I've thought about possibly writing something to monitor the error span's to see if they become visible, but not sure if that's overkill at the moment.
Thanks
Gavin
Are you using $(document).ready(...)?
if so, try to use the pageLoad() of the client side
http://encosia.com/document-ready-and-pageload-are-not-the-same/

Automatically answer a window.confirm using VBA

I have some VBA code that opens a webpage, then calls a javascript function that deletes a file off of the page. When this function is called, it calls a window.confirm in the HTML to make sure that the user really wants to delete the file. The options are yes and cancel. My question- Is there any way to automatically make my VBA code answer yes? As it is, I can automate everything but the "yes" click, which means I still have to sit there and click yes for every file that it wants to delete; it'd be nice to run the code and be able to walk away.
edit: if it helps, here is the code I'm using to execute the javascipt. Also, the javascript function code
My code:
Dim CurrentWindow As HTMLWindowProxy: Set CurrentWindow = IE.Document.parentWindow
m = 2
Do Until date_var < "2011-7-1"
Cells(1, 11).Value = Range("j1") - m
date_var = Cells(1, 11).Value
date_var = Format(date_var, "yyyy-m-d")
Call CurrentWindow.execScript("Delete('filepath_here_" & date_var & ".csv?DELETE')")
Javascript Function:
<SCRIPT LANGUAGE="JavaScript">
function Delete(What)
{
if (window.confirm("Do you really want to delete this file"))
{
location.href = What;
}
}
</SCRIPT>
I don't know VBA, but try the following script:
Call CurrentWindow.execScript("location.href='filepath_here_" & date_var & ".csv?DELETE';")
That should do the trick. It's better than calling the delete function directly. No function call overhead, no side effects, and no confirm box. Replace the last line of your code with this one and check.
You could try executing this piece of code when the page loads:
window.confirm = function() {
return true;
};

Categories