Im new for javascript / jQuery
I created a javascript / jQuery applet that I will use to display feedback from my asp.net application to the end user. Like a fancy tooltip that has slides and has different functionality, depending on whether the message has the status “error”, “warning” or “success”.
(I know that this was done before, and there are plugins and stuff to do this. I do not send this reason, I need a solution, I send a message because I need to understand what I'm doing wrong, and find out how call js / jq functions correctly.)
The asp.net codebehind method, which calls the jq function, uses RegisterClientScriptBlock (); The method works if I just put a warning ("anything"); instead of calling a popup function. If I create a dummy js function outside the document, it will work as well. But when I try to call the pop-up alert function, nothing happens. It does not even enter the function.
WITH#
public void SendPopUp()
{
string key = Guid.NewGuid().ToString();
StringBuilder javascript = new StringBuilder();
javascript.Append("$(document).ready(function() {");
javascript.Append(" AlertPopUp('Horror', 'Oh nooo, an error is simulated!', 'error');");
javascript.Append(" });");
Page page = HttpContext.Current.CurrentHandler as Page;
ScriptManager sm = ScriptManager.GetCurrent(page);
if (sm != null && sm.IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(page, typeof(Page), key,javascript.ToString() , true);
}
else
{
page.ClientScript.RegisterClientScriptBlock(this.GetType(), key,javascript.ToString() , true);
}
}
JAVASCRIPT
<script type="text/javascript">
$(function () {
var popUpId = "AlertPopUp";
var popUpCollapseId = "AlertCollapse";
var popUpDisbandId = "AlertDisband";
var titleId = "AlertTitle";
var messageId = "AlertMessage";
var bodyId = "AlertBody";
var headerId = "AlertHeader";
var imageId = "AlertImage";
var PopUpBox = $('#' + popUpId);
var CollapseDiv = $('#' + popUpCollapseId);
var DisbandDiv = $('#' + popUpDisbandId);
var TitleDiv = $("#" + titleId);
var MessageDiv = $("#" + messageId);
var Image = $('#' + imageId);
var successImagePath = "images/okej.jpg";
var alertImagePath = "images/alert.jpg";
var errorImagePath = "images/error.jpg";
var rootPathFromThisFile = "../../";
var anmationSpeed = 300;
var fadOutSpeed = 5000;
var popupWidth = PopUpBox.width();
var collapseWidth = DisbandDiv.width() + Image.width();
DisbandDiv.click(function () {
disbandPoPUp();
});
PopUpBox.click(function () {
if (state == "expanded") {
collapse();
} else if (state == "collapsed") {
expand();
}
});
$('#btnerror').click(function () {
AlertPopUp('Jättehemskt!', 'Oh nooo, an error is simulated!', 'error');
});
$('#btnalert').click(function () {
AlertPopUp('Glöm ej. ', 'Glöm ej att köpa mjölk', 'alert');
});
$('#btnsuccess').click(function () {
AlertPopUp('Woho!', 'Någonting har gått som det ska!', 'success');
});
function disbandPoPUp() {
PopUpBox.stop();
PopUpBox.css('display', 'none');
state = "off";
};
function collapse() {
PopUpBox.animate({ "right": -popupWidth + collapseWidth + 10 + "px" }, 300);
state = "collapsed";
};
function expand() {
PopUpBox.animate({ "right": "-5px" }, 300);
state = "expanded";
};
function AlertPopUp(title, message, type) {
PopUpBox.css('right', -popupWidth + "px");
PopUpBox.css('opacity', '1.0');
PopUpBox.stop();
TitleDiv.text(title);
MessageDiv.text(message);
if (type == "success") {
setBorderAndImage("green", successImagePath);
setFadeOut();
} else if (type == "alert") {
setBorderAndImage("orange", alertImagePath);
displayPopUpExpanded();
} else {
setBorderAndImage("red", errorImagePath);
displayPopUpExpanded();
}
function displayPopUpExpanded() {
PopUpBox.css('display', 'block');
expand();
}
function displayPopUpCollapsed() {
PopUpBox.css('display', 'block');
collapse();
}
function setFadeOut() {
PopUpBox.css('display', 'block');
PopUpBox.animate({ "right": "-5px" }, anmationSpeed,
function () {
state = "expanded";
startFadeKill();
}
);
function startFadeKill() {
PopUpBox.fadeTo(fadOutSpeed, 1.0, function () {
PopUpBox.fadeTo(fadOutSpeed, 0.0, function () {
disbandPoPUp();
});
});
PopUpBox.mouseenter(function () {
PopUpBox.stop();
PopUpBox.fadeTo("fast", 1.0);
});
}
}
function setBorderAndImage(color, imagePath) {
PopUpBox.css("border-color", color);
DisbandDiv.css("background-color", color);
Image.attr("src", rootPathFromThisFile + imagePath);
}
};
})
</script>