Can i use scroll and click action scripts inside alert function? - javascript

i am having a Alert function as below :
UIATarget.onAlert = function onAlert(alert)
{
var title = alert.name();
UIALogger.logWarning("Alert with title '" + title + "' encountered!");
if (title == "Installation for this product is actually free with delivery - you will not be charged for installation.")
{
alert.buttons()["OK"].tap();
return true; // bypass default handler
}
return false; // use default handler
}
here i want execute a condition, if the alert message appears i want to execute
mainWindowTarget.scrollViews()[0].scrollToElementWithName("Add to cart");
var AddButton = mainWindowTarget.scrollViews()[0].buttons().firstWithName("Add to cart").tap();
So once the alert message comes the above two line should be executed
can somebody help on this

Alert function will handle the alert which are popped up and it is automatically called. However to use scroll view and tap over it you first have to dismiss the alert pop and then come back to the screen having scroll view. But its good to experiment the same..[]

Related

Cant Seem To use var twice in function

Okay heres my problem.. i am trying to make a button onclick in the function i have defined a variable but... i want to be able to click the button to a prompt type in a website in the prompt use window.location to go to which ever site... but when the user clicks ESC or cancel in a prompt its equal to (null or false). But when i click the cancel button it goes to null as if it was a href..
function goToSite() {
var done = prompt("Please Enter a Website You'd like to go to - Or click \"Cancel\" to go back to the main page.");
window.location = done;
}
if (done === null) {
window.location = "java_doc.htm";
}
<button onclick="goToSite()"></button>
You gotta make your conditional statement run before you change window.location, otherwise your address bar will point to null. Also, your function's closing brackets are in the wrong place, and you are closing your function before it actually does its thing.
function goToSite(){
var done = prompt("Please Enter a Website You'd like to go to - Or click \"Cancel\" to go back to the main page.");
if(done === null){
window.location = "java_doc.htm";
} else {
window.location = done;
}
}
You can do the following:
function goToSite() {
var done = prompt("Please Enter a Website You'd like to go to - Or click \"Cancel\" to go back to the main page.");
window.location = (done === null) ? "java_doc.htm" : done;
}
To take it one step further you can validate if the user actually entered a valid url.
function validUrl(textval) {
var urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*#)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
return urlregex.test(textval);
}
function goToSite() {
var done = prompt("Please Enter a Website You'd like to go to - Or click \"Cancel\" to go back to the main page.");
window.location = (done === null || !validUrl(done)) ? "java_doc.htm" : done;
}
This makes sure that only if user entered a valid url, they will be taken to that url. Otherwise they will be redirected to your default page.

How do I write a JS function to disable the function under certain conditions

I'm writing a simple game in JS. It's essentially a game of chance. You press a button (which runs a function) and if it doesn't return number 9 then it shows a picture, you get a point and keep going. If it does return a 9 you see a losing picture and you loose. The game waits two seconds, (so you can actually see the losing image) and then throws an alert message telling you your high score and then refreshes the page.
The problem is between the two seconds of you loosing and receiving the alert message you can still click and receive points. Is there a way to disable the function form being called again? I am looking for a way to do this.
var didIGetAPoint = function() {
if(num != losing number) {
show pic, get a point and keep going
}
else { show losing pic, disable this function from running again, pause, then alert box.}
}
Create a boolean variable to set whether the game is over or not.
Here is my example. JSFIDDLE
HTML
<button id="pointbutton" onclick="clickforpointsandlose()">Click me to earn points and Lose!</button>
JS
var points = 0;
var isaLoser = false;
function clickforpointsandlose(){
if(!isaLoser) {
//show pic, get a point and keep going
points+=1;
console.log("total points "+ points);
}
else {
console.log("You can't earn points because you already lost.");
//show losing pic, disable this function from running again, pause, then alert box.}
}
}
console.log("You are losing in 2 seconds");
loserhaha = setTimeout(function(){
alert("You Lose!");
isaLoser = true;
}, 2000);
The simplest way of doing it would be to disable your button ( remove the onclick handler & set the button disabled attribute).
you can do something like this.
var gameover = false;
var didIGetAPoint = function() {
if(num != losing number) {
if (gameover === true) {return false;}
show pic, get a point and keep going
}
else { show losing pic, disable this function from running again, pause, then alert box.
gameover = true;
}
}

"Parameter count mismatch" is occurring on the click event of a button in asp.net

Error is coming when ever I click on the button
Microsoft JScript runtime error: Sys.ParameterCountException: Parameter count mismatch.
Function.emptyFunction = Function.emptyMethod = function Function$emptyMethod() {
/// <summary locid="M:J#Function.emptyMethod" />
if (arguments.length !== 0) throw Error.parameterCount();
}//Error is occuring here.
Yes, I am using JavaScript confirm message in this page which is included in a master page, update panel and ScriptManager as well on the client click of the button, and after clicking this button error is coming below is my code
function Confirm() {
if(document.getElementById('<%= btnSave.ClientID %>').value=="UPDATE")
{
if(document.getElementById('<%= userStatus.ClientID %>').value=="INACTIVE")
{
if (confirm("do you want to make the user INACTIVE? \n By making the user INACTIVE means its all assigned role will be revoked"))
{
document.getElementById("confirm_value").value = "Yes";
}
else
{
document.getElementById("confirm_value").value = "No";
}
}
else
{
document.getElementById("confirm_value").value = "active";
}
}
}//endFun
Actually I am using ScriptManager in the master page and Update Panel so to get rid of this error I set “ScriptMode="Release" “ of ScriptManeger and it works now no that error is not coming any more.This is the link which helped me out
http://msdn.microsoft.com/en-us/library/bb344940%28v=vs.110%29.aspx

Alert with multiple options

Just wondering, is it possible to create an alert with multiple options?
Like for example, in Facebook, when you try to close the tab/window when you have not finished typing your message, an alert with the options "Leave this page" and "Stay on this page" will pop up.
Example with form, you'are looking for window.onbeforeunload:
<script type="text/javascript">
var originalFormContent
var checkForChanges = true;
jQuery(document).ready(function () {
originalFormContent = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize();
});
function onClose() {
if (checkForChanges && originalFormContent != "undefined") {
var content = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize();
if (content != originalFormContent) {
return confirm('You have unsaved changes. Click OK if you wish to continue ,click Cancel to return to your form.');
}
}
}
window.onbeforeunload = onClose();
You're referring to window.onbeforeunload:
Best way to detect when a user leaves a web page?
You can also use the window.confirm() function for OK/Cancel options:
http://jsfiddle.net/UFw4k/1
Other than that, you'd have to implement a custom modal alert, such as jQuery Dialog.
Please have a look at http://jqueryui.com/dialog/#default
Copied this from a previous answer: JavaScript alert with 3 buttons

How to handle alert in UI Automation using javascript?

In my application after tapping on a one button it gives and alert.There are two button on alert window: 1. Cancel 2. Ok
I have tried to tap on OK by using the solution given on the forum but it dosen't work.
UIATarget.onAlert = function onAlert(alert) {
var title = alert.name();
UIALogger.logWarning("Alert with title '" + title + "' encountered!");
if (title == "Attention")
{
alert.buttons()["OK"].tap();
return true; // bypass default handler
}
return false; // use default handler
}
Function for handling alert dosen't called.Can anyone help me on this issue?
Thanks in advance.
UIATarget.onAlert = function onAlert(alert)
{
UIATarget.localTarget().delay(1);
UIALogger.logMessage("alertShown");
target.captureScreenWithName("AlertCaptured");
return true;
}
app.alert().buttons()["OK"].tap();
My solution to this problem was to add an one second delay after the function that handles the alert. You can not end your script with that function.
UIATarget.onAlert = function onAlert(alert) {
var title = alert.name();
UIALogger.logWarning("Alert with title '" + title + "' encountered.");
if (title == "Are you sure you want to delete this?") {
alert.buttons()["Delete"].tap();
return true; //alert handled, so bypass the default handler
}
return false;
}
target.delay(1);

Categories