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.
Related
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.
I am using web socket connection for communication. But when user refresh page, I want to close this web socket connection.
I also using window.onbeforeunload event to show confirmation box to user with customized text. If user selects 'do not reload' option then no action (which is working fine)
But I want to close web socket connection if user selects reload option. I don't know how to achieve this. I tried to call window.unload and tried to close web socket connection but its not working. My code is as follows.
window.onbeforeunload = function(e) {
return "you are closing this web page';
};
window.unload = function(e) {
webSockect.closeConnection();
};
Please advise how to achieve this.
Window events are prefixed with on.
Try to use window.onunload = ... or even better
window.addEventListener('unload', ...).
When using addEventListener you specify the event name without on prefix.
Read more: WindowEventHandlers.onunload
First of all, there is a syntax error in the code above, you have written:
window.onbeforeunload = function(e) {
return "you are closing this web page';
};
So, type of quotes there should be either single or double, same at start and end of the string.
It is supposed to be, and it will work:
window.onbeforeunload = function(e) {
return "you are closing this web page";
};
====
Back to the point. All you can do with unload prompt is to change the Message. Anything otherwise doesn't work like:
Changing the action on Buttons on the prompt
Changing the Button text of the prompt
Changing the UI of the box
Q. even you write something before the return if the function, that will be ignored by the browser.
You can in fact run a method on close if you assign the method to a variable and then return the variable.
function unloadWebSocket()
{
webSockect.closeConnection();
}
window.onbeforeunload = function(e)
{
var x = unloadWebSocket();
return x;
};
While I have no experience with websockets, I know this works with synchronous ajax requests.
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?
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;
};
I have a Flex application which allows the user to edit a cloud-based document. (Think SlideRocket.) When the user tries to navigate away or close the browser window, I'd like to show them an are-you-sure dialog iff they have unsaved changes.
I'm using the following custom class, which I found at Flash player notified on browser close or change page (as3). I don't think it is the problem.
package
{
import flash.external.ExternalInterface;
public class ExternalInterfaceUtil
{
public static function addExternalEventListener(qualifiedEventName:String, callback:Function, callBackAlias:String):void
{
// 1. Expose the callback function via the callBackAlias
ExternalInterface.addCallback( callBackAlias, callback );
// 2. Build javascript to execute
var jsExecuteCallBack:String = "document.getElementsByName('"+ExternalInterface.objectID+"')[0]."+callBackAlias+"()";
var jsBindEvent:String = "function(){"+qualifiedEventName+"= function(){"+jsExecuteCallBack+"};}";
// 3. Execute the composed javascript to perform the binding of the external event to the specified callBack function
ExternalInterface.call(jsBindEvent);
}
}
}
In my applicationComplete function, I add an event listener to the javascript window.onbeforeunload event, as follows:
ExternalInterfaceUtil.addExternalEventListener("window.onbeforeunload", requestUnloadConfirmation, "unloadConfirmation");
The Actionscript function requestUnloadConfirmation (below) is successfully called when the user tries to close the browser window. However, it does not prevent the browser from closing. (In Chrome, the browser closes and the Actionscript function is called subsequently. In Firefox, the browser stays open for the duration of the function but then closes.)
private function requestUnloadConfirmation():String {
if (changedSinceSave)
return "There are unsaved changes. Are you sure you want to leave without saving?";
else
return null;
}
Behavior is identical in both debug and release builds, and on the production server as well as the local machine.
Any help would be greatly appreciated,
Dave
Right now, when the JavaScript event is fired, it is set to call your function in your AS3 code, which it does. The JavaScript function, however, is not returning the value that your AS3 function returns. To get this behaviour, add 'return' to the JavaScript event-handling function created in addExternalEventListener like so:
var jsBindEvent:String = "function(){"+qualifiedEventName+"= function(){return "+jsExecuteCallBack+"};}";
Since the event handler should return a true or false value, your requestUnloadConfirmation function should have a return type of Boolean and return false to cancel the event, and true otherwise. Use the following to get a confirmation dialog box:
private function requestUnloadConfirmation():Boolean {
if (changedSinceSave)
return ExternalInterface.call("confirm", "There are unsaved changes. Are you sure you want to leave without saving?");
else
return false;
}
UPDATE:
It turns out that returning a string to window.onbeforeunload causes a confirmation dialog box to be shown automatically. The ExternalInterface.call to confirm causes a second dialog box to show; it is redundant. The only change required in the AS3 code is to add the "return" in the generated JavaScript.
In a regular html/javascript web-app you would use the window.onbeforeunload event to do this.
https://web.archive.org/web/20211028110528/http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm
Perhaps you can use this event, and check some value of your flex app to determine if you should ask the user (not familiar with flex...)?
I had to run some modifications in the above code to make it work.
The problem was due to the fact that the function which handles the event window.onbeforeunload, should not return any value to avoid popup confirmation and should return text value when a popup confirmation is in-order
Here are my changes:
private function requestUnloadConfirmation():String {
if (changedSinceSave){
return "There are unsaved changes. Are you sure you want to leave without saving?";
}
return null;
}
And a little change in embedded JS
var jsBindEvent:String = "function(){"+qualifiedEventName+"= function(){ if ("+jsExecuteCallBack+") return "+jsExecuteCallBack+"};}";