I am making in which I want to display alert box and then redirect it to some other page in asp.net
My code is
var s = Convert.ToInt32(Session["id"].ToString());
var q = (from p in db.students
where p.userid == s
select p.sid).SingleOrDefault();
if (q == 0)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "reg();", true);
}
this is my reg() function
function reg() {
var con = alert('Please Register Your Self Fisrt ..!! Thank you');
window.location = "reg.aspx";
}
My code is working fine but alert box gets disappear after some second without clicking on it because of that user is unable to read alert message
I want to redirect it to reg.aspx but after clicking OK on alert box ..
please help
Firing a confirm box instead of alert will do what you are trying to achieve. You will have the option to redirect conditionally or unconditionally
function reg(){
var con=confirm("You need to register first before proceeding. Redirect to Registration Page");
if(con==true){
window.location = "reg.aspx";
}
else{
//if redirect unconditionally
window.location = "reg.aspx";
}
}
Related
I have a php script with a an image that when you click on it you are redirected to another page.
I also have an ajax / jquery check to see if you are logged in or not.
So, when someone clicks on the link, ajax finds out if they are logged in, and if not they get an alert box.
My link has the id='newlisting'
$("#newlisting").click( function() {
$.post("ajaxqueries.php", {"checkloggedin": ''}, function(data) {
var loggedin = data;
if ( loggedin == 0 ) {
alert('Please login or register to use this feature');
}
});
});
The problem is that when they click on on the alert box, it continues to the screen as if you were logged in.
I need the php script to exit if they get the alert box and hit ok.
Any help would be appreciated.
You need to prevent the redirect.
$("#newlisting").click( function( event ) {
event.preventDefault();
$.post("ajaxqueries.php", {"checkloggedin": ''}, function(data) {
var loggedin = data;
if ( loggedin == 0 ) {
alert('Please login or register to use this feature');
}else{
// Redirect code goes here.
}
});
});
Bootstrap Warnings Image I have two different types of bootstraps alerts (warning and danger). Danger alerts are always suppose to be on the page no matter what. Warning alerts happen when user clicks on the dropdown list carriers it displays a bootstrap warning notification. User has to click on 'x' for it to close. I need it to work when user click anywhere on the page or by clicking on the 'x'.
HomeController.cs
case "Carrier":
var carrierid = (from foo in db.Carriers
where foo.ID == warningid
select foo.WarningID).Single();
if (carrierid != null)
{
warning = (from warnings in db.Warnings
where warnings.IsActive == true && warnings.Id == carrierid
select warnings.WarningBody).SingleOrDefault();
if (warning != null)
{
warning = ("<div class=\"alert alert-warning alert-dismissible\" id=\"myWarning\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">×</span></button><strong>" +
warning + "</strong></div>");
}
else
{
warning = "";
}
}
else
{
warning = "";
}
return Json(warning, JsonRequestBehavior.AllowGet);
default:
break;
warningwriter.js
//// warning display script takes a value of warningid and warningcaller
$(document).ready(function () {
var warningid = 0;
var warningcaller = "Universal";
loadWarnings(warningid, warningcaller);
});
$('#Phones').change(function () {
var warningid = $(this).val();
var warningcaller = "Phone";
loadWarnings(warningid, warningcaller);})
$('#Carriers').change(function () {
var warningid = $(this).val();
var warningcaller = "Carrier";
loadWarnings(warningid, warningcaller);})
function loadWarnings(warningid, warningcaller) {
$.getJSON("../Home/LoadWarnings", { warningID: warningid, warningCaller: warningcaller },
function (warning) {
var select = $('#warnings');
select.append(warning);
});
};
As Martin suggested, it's something you need to do in javascript. I haven't tested this, but it would be something like:
$(document).click(function (event) {
$(".alert").hide();
});
This is basically, clicking anywhere on the page will hide any displayed alert.
Since you have two different types of bootstraps alerts (danger and warning). You have to use ".alert-warning" because that is the one you want to get rid of when user did a mouse click anywhere on page. ".alert" is all of the bootstraps alerts, however, if you need to get rid of a certain type you can call the contextual classes(e.g., .alert-success, .alert-info, .alert-warning, and/or .alert-danger. https://v4-alpha.getbootstrap.com/components/alerts/
$(document).click(function (event) {
$(".alert-warning").hide();
});
$(document).ready(function () {
$("#myWarning").click(function () {
$(".alert").alert("close");
});
});
By doing this, u are making two things wrong:
You are binding the click event to an element, that possibly
doesnt exist when the page is loaded.
You are binding the click
event to a restricted element. This means that the alert wont be
closed when u click anywhere on the page. In this case, only clicks on #myWarning will close the alert.
Finally, you should use what #Bryan already posted :)
Edit:
Assuming that u have a set of alerts that u always want to close on page load, add to this elements a way to identify them, for example a class "close-on-screenclick"
$(document).click(function () {
$(".close-on-screenclick.alert").alert("close");
});
.This should close those elements whenever a click is made on the screen
I'm trying to stop the submit if an error exists and go through the submission if no error exist.
This is just a section of the code. Regex works correctly, also the error texts were showing up too. The thing is that now I'm trying to control the submit proccess and due to an error (console is not showing up anything) the error text is not showing up and the submission is just going through even if an input error exists.
There is a mistake that I cannot figure out (No jquery please)
var sub = document.querySelector("#register input[type='submit']");//the submit botton
var err_text_1 = "First name must be filled out!";
sub.onsubmit = function(){
if (first_name == null || first_name == "") {
err_holder_1.innerHTML = err_text_1;
return false;
}
else {
err_holder_1.style.display = "none";
}
}
To prevent submit add event in the function and return false like so:
sub.onsubmit = function (event) {
// Will prevent submit
event.preventDefault();
return false;
};
Also select the form you are submitting not the button itself. So:
var sub = document.getElementById('ActionForm');
Hope this helps.
You need to select the form like
var sub = document.querySelector("#register");//If register is your form id Not input[type='submit']
I need a confirmation box to pop up when a user hits a Save button on my webpage. I currently have a confirm box showing up whenever a user hits ANY button. I need it strictly for the one. The button I need it linked to is called btnsavesurvey.
Here is my current code in the page_load:
Dim message As String = "Do you want to complete survey?"
Dim sb As New System.Text.StringBuilder()
sb.Append("return confirm('")
sb.Append(message)
sb.Append("');")
ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
$('#btnsavesurvey').click(function(){
var r = confirm("Your message");
if (r == true) {
alert("You pressed OK!");
// Do something when the user pressed ok
} else {
alert("You pressed Cancel!");
// Do something when the user pressed cancel
};
});
If your btns id is btnsavesurvey if its class is btnsavesurvey than use .btnsavesurvey
In my application when user logs out the browser is closed. And on browser close I am throwing an alert. Now what I want is if I directly close the browser window alert should come but if window is closed through logout alert should not come as I have shown another confirm message of logout.
function closeEditorWarning(){
for (var i=0;i<childWindow.length;i++) {
if (childWindow[i] && !childWindow[i].closed) childWindow[i].close();
if(i==0) {
alert("This will close all open e-App applications");
}
}
window.close();
}
window.onbeforeunload = closeEditorWarning;
And this is my logout code
$('#'+id).click(function(event){
event.preventDefault();
$('#centerContent').load('<%=request.getContextPath()%>/'+target);
});
} else {
$('#'+id).click(function(event){
event.preventDefault();
var r=confirm("logout");
if (r==true)
{
flag=true;
for (var i=0;i<childWindow.length;i++) {
if (childWindow[i] && !childWindow[i].closed) childWindow[i].close();
}
window.close();
} else { }
});
}
Use a flag. Set the flag when log OFF is clicked.
Check the flag before showing the alert
set a hidden field with user logon status and on close check:
if($('#isUserLogIn').text()=='true')
{
//show alert
}