Category: Javascript


javascript:

var map;
var geocoder;
var bounds;

var _count = 0;
var _max = 0;

var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;

function InitializeTimer() {
secs = 100;
StopTheClock();
StartTheTimer();
}

function StopTheClock() {
if (timerRunning)
clearTimeout(timerID);
timerRunning = false;
}

function StartTheTimer() {
if (secs == 0) {
StopTheClock();
}
else {
if (_max == _count) {
SetZoom();
StopTheClock();
}
else {
self.status = secs;
secs = secs – 1;
timerRunning = true;
timerID = self.setTimeout(“StartTheTimer()”, delay);
}
}
}

function SetZoom() {
map.setZoom(map.getBoundsZoomLevel(bounds));
map.setCenter(bounds.getCenter());
}

function initializeGoogleMap(address, control) {
if (GBrowserIsCompatible()) {
bounds = new GLatLngBounds;
geocoder = new GClientGeocoder();

map = new GMap2(document.getElementById(control));

map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

map.setCenter(new GLatLng(0, 0), 0);

var addresses = address.split(“;”);

_count = 0;
_max = addresses.length;

for (var i = 0; i < addresses.length; i++) {
AddAddress(addresses[i]);
}

InitializeTimer();
}
}

function AddAddress(address) {
var data = address.split(“~”);
geocoder.getLatLng(
data[1],
function (point) {
if (!point) {
_count++;
}
else {
var marker = new GMarker(point);
map.addOverlay(marker);
bounds.extend(marker.getPoint());

_count++;
var html = “<b>” + data[0] + “</b><br />” + data[1] + “<br />Phone: ” + data[2] + “, Fax: ” + data[3];
GEvent.addListener(marker, ‘click’, function () {
marker.openInfoWindowHtml(html);
});
}
}
);
}

Markup:
<asp:Literal ID=”litMapScript” runat=”server”></asp:Literal>
<div>
<div id=”map” runat=”server” style=”width: 570px; height: 290px;”>
</div>
</div>

C# :
string locations = “”;
list<Location> allLocation = all locations;
foreach (Location l in allLocation)
{
if (locations == “”)
locations += l.Title + “~”+ l.Address + “~” + l.Phone + “~” + l.Fax;
else
locations += “;” + l.Title + “~” + l.Address + “~” + l.Phone + “~” + l.Fax;

}

litMapScript.Text = string.Format(@”<script src=’http://maps.google.com/maps?file=api&amp;v=2&amp;key={0}&sensor=false’
type=’text/javascript’></script>”, ConfigurationManager.AppSettings[“MAPKEY”]);
Page.ClientScript.RegisterStartupScript(this.GetType(), “javaScript”, string.Format(“initializeGoogleMap(‘{0}’,'{1}’);”, locations, map.ClientID), true);

ConfigurationManager.AppSettings[“MAPKEY”] -> google map key

Advertisements

javascript:

var OriginalValidatorUpdateDisplay = null;
function NewValidatorUpdateDisplay(val) {

OriginalValidatorUpdateDisplay(val);
if (val.controltovalidate) {
if (!CheckValidatorsForControl(document.getElementById(val.controltovalidate))) {
if (document.getElementById(val.controltovalidate).toString() == “[object HTMLSelectElement]“)
document.getElementById(val.controltovalidate).style.backgroundColor = “#FFAAAA”;
else
document.getElementById(val.controltovalidate).style.background = “#FFAAAA”;
}
else {
if (document.getElementById(val.controltovalidate).toString() == “[object HTMLSelectElement]“)
document.getElementById(val.controltovalidate).style.backgroundColor = “”;
else
document.getElementById(val.controltovalidate).style.background = “#FCFCFC”;
}
}
}

if (typeof (ValidatorUpdateDisplay) == ‘function’) {
OriginalValidatorUpdateDisplay = ValidatorUpdateDisplay;
ValidatorUpdateDisplay = NewValidatorUpdateDisplay;
}

function CheckValidatorsForControl(control) {
for (var i = 0; i < control.Validators.length; i++) {
if (!control.Validators[i].isvalid) {
return false;
}
}
return true;
}

function fnOnUpdateValidators() {
for (var i = 0; i < Page_Validators.length; i++) {

var val = Page_Validators[i];
var ctrl = document.getElementById(val.controltovalidate);

if (ctrl != null && ctrl.style != null) {

if (!val.isvalid) {
if (ctrl.toString() == “[object HTMLSelectElement]“)
ctrl.style.backgroundColor = “#FFAAAA”;
else
ctrl.style.background = “#FFAAAA”;
}
else {
if (ctrl.toString() == “[object HTMLSelectElement]“)
ctrl.style.backgroundColor = “”;
else
ctrl.style.background = “#FCFCFC”;
}
}
}
}

C# code :

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), “val”, “fnOnUpdateValidators();”);
}

Javascript:

function lbIframeClose() {
parent.document.getElementById(“lbMain”).style.display = “none”;
parent.document.getElementById(“lbOverlay”).style.display = “none”;
}

Markup:

<a href=”javascript:lbIframeClose();”>Close Window</a>

javascript:

function isDate(s) {
if (s.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)
return false;

s = s.replace(/[\-|\.|_]/g, “/”);

var dt = new Date(Date.parse(s));

var arrDateParts = s.split(“/”);
return (
dt.getMonth() == arrDateParts[0] – 1 &&
dt.getDate() == arrDateParts[1] &&
dt.getFullYear() == arrDateParts[2]
);
}

javaScript :

function DefaultButton(event) {
if (event.keyCode == 13) {
var b = document.getElementById(‘<%= lbLogin.ClientID %>’);
if (b && typeof (b.click) == ‘undefined’) {
b.click = function() {
var result = true;
if (b.onclick) result = b.onclick();
if (typeof (result) == ‘undefined’ || result) {
eval(b.getAttribute(‘href’));
}
}
}
}
}

Markup:
<asp:Panel ID=”Panel1″ runat=”server” DefaultButton=”lbLogin”>
<asp:LinkButton ID=”lbLogin” runat=”server” OnClick=”lbLogin_Click”>
<img src=”images/btn_login_go.gif” width=”21″ height=”22″ alt=”Go” title=”Go” />
</asp:LinkButton>
<asp:TextBox ID=”txtPassword” CssClass=”input_text” runat=”server” TextMode=”Password” onkeypress=”javascript:DefaultButton(event);”/>
<label for=”<%= txtPassword.ClientID %>”>
Password:</label>
<asp:TextBox ID=”txtUserName” CssClass=”input_text” runat=”server” onkeypress=”javascript:DefaultButton(event);” />
<label for=”<%= txtUserName.ClientID %>”>
Login Name:</label>
</asp:Panel>

Trim

String.prototype.trim = function () {
return this.replace(/^\s*/, “”).replace(/\s*$/, “”);
}

var fontSize = 12; // default size of the font
function zoomIn() {
fontSize += 1;
document.body.style.fontSize = fontSize + “px”;
}
function zoomOut() {
fontSize -= 1;
document.body.style.fontSize = fontSize + “px”;
}

reset_form_value = function (str1, str2, val) {
if (str1 == val.value.trim())
val.value = str2;
}
Markup:

<asp:TextBox ID=”txtTitle” runat=”server” onfocus=”reset_form_value(‘Title’, ”, this)” onblur=”reset_form_value(”, ‘Title’, this)” Text=”Title” />

function ReplaceAll(value, oldValue, newValue) {
while (1) {
var i = value.indexOf(“oldValue”);
if (i == -1)
break;
value = value.replace(“oldValue”, “newValue”);
}
return value;
}