Close popup on cancel of javascript button. - javascript

In one modal popup, I am updating values, and upon save button click, before updating I am asking user by javascript message box : "Are you sure....?"
This I am doing by following code:
btnSave.Attributes.Add("onclick", "javascript:return " & "confirm('Are you sure " & RbtnConfirm.SelectedValue & " ' )")
But when user clicks cancel, I want to close that whole modal popup by:
modalpopup1.hide() method
I am confused how and where should I include this line of code?
From where can I get whether btnSave.Attributes.Add("onclick", "javascript:return " & "confirm('Are you sure " & RbtnConfirm.SelectedValue & " ' )") has returned false or true?
Please help me.

why the return " & "?
Anyway for inline (which is not recommended), just do
btnSave.Attributes.Add("onclick", "if (confirm('Are you sure " & RbtnConfirm.SelectedValue & " ' )) modalpopup1.hide()")

Move the confirm call to separate function, ie.:
include this in your .ascx/.aspx:
<script>
function myconfirm(sValue) {
var bValue = confirm(sValue);
if (!bValue) {
modalpopup1.hide();
}
return bValue;
}
</script>
in codebehind:
btnSave.Attributes.Add("onclick", "javascript:return " & "myconfirm('Are you sure " & RbtnConfirm.SelectedValue & " ' )")

Related

Javascript and Escape Character on Android Webview

i need to be able to capture user's password and then bypass it if user authorized with biometrics to Android WebView. I'm successfully capturing the password and passing it to the webview, I'm executing the js like this, the method returns me js with injected css selector and value to input which I run against the webview
fun getJsToEnterValueToTextField(cssSelectorById: String, value: String): String {
val js = "javascript:(function() { " +
"var element = document.getElementById(\"$cssSelectorById\");" +
"console.log(element);" +
"element.value='${value.trim()}';" +
" element.dispatchEvent(new Event('input', {\n" +
" view: window,\n" +
" bubbles: true,\n" +
" cancelable: true\n" +
" }))" +
"})()"
Timber.d("SWA_WEBVIEW - getJsToEnterValueToTextField >> $js")
return js
}
//--//
webview.loadUrl(url2load)
The problem happens if the user has any escape characters in the value,
for example if user has '%E9' as part of that password string, once I run that js > this %E9 would transform to é
Does anyone know if there is a way around it? I searched all over but nothing seems to make a difference.

Getting values outside a HTML form

Inside my content.js I am writting a new HTML page with a pre polulated form, which contains var a and var b. Those 2 variables are created before, inside content.js, so I can easily use them inside my HTML page. Now I want to override those variables a and b as the user finishes editing the form and presses the button Accept. Is there anyway I can achieve this?
This is a part of the code
var a="FName";
var b="LName";
var myWindow = window.open("Accept", "myWindow", "width=450, height=300");
myWindow.document.write(
"<html>"+
"<head>"+
'<script> function closeWindow(){ var x = document.getElementById("firstname").value alert(x); window.close();}'+
"</script>"+
"</head>"+
"<body>"+
"<form>"+
"<div id=leadinformation>"+
"<p id=titleParagraph>You are about to create a new Lead:</p>"+
"First Name....."+ "<input type=text id=firstname value="+a+">" +"<br>"+
"Last Name....."+ "<input type=text id=lastname value="+b+">" +
"</div>"+
"<div>"+
"<button id=Accept onClick=closeWindow() >Accept</button>"+
"<button id=Close onClick=closeWindow() >Close</button>"+
"</div>"+
"</form>"+
"</body>"+
"<html>"
);
myWindow.document.getElementById('Accept').onclick=Accept;
myWindow.document.getElementById('Close').onclick=Close;
function Accept(){
alert(myWindow.document.getElementById('firstname').innerText);
}
function Close() {//do smthing
}
Sorry for bad formating.
Currently the output of the Accept(); is empty at the moment. How can I get first name input result?
What I want to achieve:
1) I am creating a button on a page
2) When I click on the button a new html page pops out (the one that I am hardcode writing it)
3) I pre populate the form with some variables that I created before
4) When Clicking The Accept button on the form the Accept() function is triggered where I would want to use those input values the user has written.
This should help you: Sharing global javascript variable of a page (...).
The question is about "How to share a variable to another page in an iframe", but this works for a new windows as well.
i.e.:
myWindow.document.write(
"<html>" +
"<head>" +
'<script>' +
'function closeWindow(){' +
'var x = document.getElementById("firstname").value;' +
'alert(x);' +
'// do something to parent.a and parent.b here, just because you can:' +
'parent.a = "Oh, would you look at it, it works!";' +
'parent.b = "And it is so pretty too!";' +
'window.close();' +
'}' +
"</script>" +
"</head>" +
" Your body code here, etc " +
"</html>"
);
Also, please note that your code lacks a semicolon (;) after inserting value to var x in your new window's JavaScript code. That will most probably make your code malfunction. The closing </html> tag lacks the slash, but I don't know if that's gonna break anything; you'd better fix that as well, just in case.

JavaScript onClick attribute into variable

var user = [];
function doFunction()
{
for(l=0;l<3;l++)
{
user.push(prompt("Bitte Mindestens 6 Zahlen von 1-49 eingeben" + "Abfrage Nr. " + (l + 1)));
}
}
doFunction();
document.write("Ihre Eingaben: " + user);
This is the working code, but I dont want that the function gets activated after refreshing the site, so I added this button:
and deleted doFunction(); in the script so it looked kinda like this:
<input type="button" value="Viel Glueck!" onClick="doFunction();">
<script>
var user = [];
function doFunction()
{
for(l=0;l<3;l++)
{
user.push(prompt("Bitte Mindestens 6 Zahlen von 1-49 eingeben" + "Abfrage Nr. " + (l + 1)));
}
}
document.write("Ihre Eingaben: " + user);
</script>
the script gets executed after pressing the button, but the variable is not working anymore(it doesnt save the output)
can anyone please help me with my problem?
As already pointed out, you might want to move the document.write into your function. However, I would like to point out that using document.write to begin with is a bad idea in almost all situations.
Why? It only works while the page is "open", i.e. is being loaded. When the user press the button the page will most likely have finished loading. The browser will then reopen it, and in doing so erasing all content. Read more about it here, or check this fiddle out.
Instead, if you want to write something to the page using JavaScript, use the DOM. First create a div that will contain the content:
<div id="message" />
Then when you want to write something, in your case in the doFunction, fill the div with content like this:
document.getElementById("message").innerHTML = "Ihre Eingaben: " + user;
Try to move the document.write inside the function like this:
var user = [];
function doFunction()
{
for(l=0;l<3;l++)
{
user.push(prompt("Bitte Mindestens 6 Zahlen von 1-49 eingeben" + "Abfrage Nr. " + (l + 1)));
}
document.write("Ihre Eingaben: " + user);
}

ASP.NET C# backend, did the user press OK or Cancel from JavaScript confirm box?

In the C# backend code for a website, I have the following code that displays a message for the user, enumerating the data they are about to submit, and asking for human-confirmation that the information appears accurate.
This is executed within C#, not within JavaScript, so how can I access the result of the confirm function?
My C# logic needs to follow a different path if they click OK, versus if they click Cancel.
I'm working on a very old application (13 years), that has been untouched for quite some time.
As a result, I don't have the ability to alter the general design of the application.
ClientScript.RegisterStartupScript(this.GetType(), "InvalidEntrySubTypeAlert", "confirm('" +
"Are you sure you want to create the following deal?\n" +
"\nDeal ID :\t\t\t" + nDealID +
"\nCompany Name :\t\t" + txtCompanyName.Text +
"\nEntry Subject :\t\t" + txtEntrySubject.Text +
"\nBusiness Lead :\t\t" + ddlBusinessLead.SelectedItem.Text +
"\nLicense Status :\t\t" + ddlLicenseStatus.SelectedItem.Text +
"\nEffective Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calEffectiveDate.SelectedDate) +
"\nExpiration Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calExpirationDate.SelectedDate) +
"\nLicense Location :\t\t" + txtLicenseLocation.Text +
"\nEntry Sub Type :\t\t" + ddlEntrySubType.SelectedItem.Text.Split(' ')[0]
+ "');", true);
You can use a HiddenField to set the value from the client side and then access it on the Server.
Here is a mockup
<asp:HiddenField ID="hndSetFromJS" runat="server"></asp:HiddenField>
And set the value like this
document.getElementById("#<%= hndSetFromJS.ClientID %>").value = "yourValue";
or
$("#<%= hndSetFromJS.ClientID %>").val("yourValue"); // Jquery
I think I would call a page method (a method in the code behind marked with webmethod attribute) from client side or use ajax to call a web service.
Since the confirm action is executed client side there's no direct link to your server side cide
You can create 2 functions in client side
function functionOK()
{
//some code
}
function functionCancel()
{
//some code
}
and change your code like this
ClientScript.RegisterStartupScript(this.GetType(), "InvalidEntrySubTypeAlert", "if(confirm('" +
"Are you sure you want to create the following deal?\n" +
"\nDeal ID :\t\t\t" + nDealID +
"\nCompany Name :\t\t" + txtCompanyName.Text +
"\nEntry Subject :\t\t" + txtEntrySubject.Text +
"\nBusiness Lead :\t\t" + ddlBusinessLead.SelectedItem.Text +
"\nLicense Status :\t\t" + ddlLicenseStatus.SelectedItem.Text +
"\nEffective Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calEffectiveDate.SelectedDate) +
"\nExpiration Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calExpirationDate.SelectedDate) +
"\nLicense Location :\t\t" + txtLicenseLocation.Text +
"\nEntry Sub Type :\t\t" + ddlEntrySubType.SelectedItem.Text.Split(' ')[0]
+ "')){ functionOK();} else{functionCancel()}", true);

write two functions plus text using innerHTML

I'm embarassed to ask but I need help getting these scripts to work together and output what I'm looking for:
HEAD section, first script:
function copyToClipboard(s) {
if (window.clipboardData && clipboardData.setData) {
clipboardData.setData('text', s);
}
}
HEAD section, second script:
var name=prompt("Enter your first and last name please.","name");
function displayDate()
{
document.getElementById("time").innerHTML="Edited on " + Date() + " by " + name + ";
}
BODY section:
<button type="button" onclick="displayDate()">Display Date</button>
<strong><span id="time">Date and time will display here</span></strong>
So basically what I'm trying to accomplish here is a webpage that prompts the user to enter their first and last name when the page loads. Then when they click the button that's labeled "Display Date", the span labeled "time" should change to something like:
Edited on Thu Jan 01 12:59:59 2099 by John Doe
The way I have the current script is giving me a "Unterminated string constant" error when the page loads. When I click the button I get an "Object expected" error.
Can anyone provide any suggestions please?
function displayDate()
{
document.getElementById("time").innerHTML="Edited on " + Date() + " by " + name + ";
}
You do have an unterminated string. Check after the name variable.
var name=prompt("Enter your first and last name please.","name");
function displayDate()
{
document.getElementById("time").innerHTML="Edited on " + Date() + " by " + name;
}

Categories