Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts
7
Posted on 4:17 AM by prajeesh and filed under ,
Sometimes you may be want to show your latest twitter tweets in your website or blog, most of the cases you are doing this by using widgets with limited customization facilities and showing ads or links to other websites, here is an easier way to achieve this using twitter API and javascript.
Step 1:
First, decide where about on your page you want your last tweet to display. Then paste following html code there.
Step 2:
Next you need to put these 2 lines of JavaScript below the code in step 1. On the 2nd line of code where it says prajeeshkk.json, you need to replace prajeeshkk with your twitter username.

Step 3:(Optional)
You can apply css and make the div displaying the tweet stylish.


See how my tweet design looks :

Shout it kick it on DotNetKicks.com
0
Posted on 12:55 PM by prajeesh and filed under , ,
In some websites you may seen animating or scrolling page titles, here is the trick to do this, just place below code between your page's <head> and </head> tags
Shout it kick it on DotNetKicks.com
0
Posted on 1:24 PM by prajeesh and filed under ,
In ASP.net we are using Response.Redirect or Server.Transfer for redirect to another page, this Redirection can also be done using Javascript or HTML
javascript:

< language="javascript">
window.location = "YourURL.aspx";
< /script >

Plain HTML(you can add following code between your <head> and </head> tags):

< equiv="REFRESH" content="0;url=yourURL.aspx">
Shout it kick it on DotNetKicks.com
0
Posted on 1:29 PM by prajeesh and filed under , , ,
Me and my colleague Anurag were trying to integrate a Paypal button in one of our recent project, we copied the button code available from Paypal website to our ASP.NET page and it was not worked and button click results only in a postback, at last we realized that it wont work as button code contained a form and ASP.net will not support more than one form in a page.
The button code we got from Paypal was like below:








We googled a lot to overcome this situation and finally we got a quick solution from Chyake Uchaya's blog, but the solution was not worked well in Mozilla, we done minor changes in the code and it worked perfectly finally, here is the code:





if you are not using Master pages, you can replace getElementById('aspnetForm') with getElementById('form1') or the form name you are using. Shout it kick it on DotNetKicks.com
0
Posted on 4:32 AM by prajeesh and filed under ,
In a previous post i have explained about calling a web service from javascript, please see this post to see calling a web service without parameters.
Suppose you have a web service that accepts user name and password and returns a value in XML format and you want to show the returned value in a webpage or a widget.

For example :

<?xml version="1.0" encoding="utf-8" ?>
<int xmlns="http://tempuri.org/">1</int>

You can use following code snippet to call a web service from javascript:
<script language="text/javascript">
var returned_data;
var request = new XMLHttpRequest();
//function to initialize web service
function initializeWebservice()
{
var url="http://2test.hopto.org/Leadservice.asmx/GetLeadCounts?UserName=Prajeesh&Password=123456";; //Web service url
request.onreadystatechange = webStatusProc;
request.open( "GET", url, true );
request.send();
}
//function to process webservice response
function webStatusProc()
{
if (request.readyState == 4) // Request completed?
{
response = request.responseXML.toXML();
XML = XMLDOM.parse( response);
returned_data= XML.evaluate('string(/int)');
alert(returned-data);
//do anything with returned data
}
}
</script> Shout it kick it on DotNetKicks.com
0
Posted on 2:46 PM by prajeesh and filed under ,
Suppose you have a web service that returns a value in XML format and you want to show the returned value in a webpage or a widget.
For example :

<?xml version="1.0" encoding="utf-8" ?>
<int xmlns="http://tempuri.org/">1</int>

You can use following code snippet to call a web service from javascript:
<script language="text/javascript">
var returned_data;
var request = new XMLHttpRequest();
//function to initialize web service
function initializeWebservice()
{
var url =
http://Localhost/Testwebservice.asmx/GetLeadCounts; //Web service url
request.onreadystatechange = webStatusProc;
request.open( "GET", url, true );
request.send();
}
//function to process webservice response
function webStatusProc()
{
if (request.readyState == 4) // Request completed?
{
response = request.responseXML.toXML();
XML = XMLDOM.parse( response);
returned_data= XML.evaluate('string(/int)');
alert(returned-data);
//do anything with returned data
}
}
</script>



Shout it kick it on DotNetKicks.com
1
Posted on 6:20 AM by prajeesh and filed under , ,
Printing is a common feature needed in almost every type of web pages, there is a plenty of options to include print feature in your web page, i would like to describe two methods.

1.Printing Using java script
It is a simple task

This HTML code will return a Print button and you can click this button to print your web page.

2.Printing using web controls using ASP.net

Little programming effort is needed for printing web controls in your ASP.net application.
If you want to print contents of a web control in your ASP.net page, you can use following code segment, it is also using javascript for printing but it prints selected control only.
///
/// Prints any web control Grid view, DataGrid, Page , Panel etc.
///
///

public static void PrintWebControl(Control ctrl)
{
String Script;
StringWriter stringWrite = new StringWriter ();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
if ( ctrl is WebControl )
{
Unit w = new Unit(400, UnitType.Pixel);
((WebControl)ctrl).Width = w;
}
Page pg = new Page ();
if (Script != string.Empty )
{
pg.RegisterStartupScript ("PrintJavaScript",Script);
}
HtmlForm frm = new HtmlForm ();
pg.Controls.Add (frm);
frm.Attributes.Add ("runat","server");
frm.Controls.Add (ctrl);
string scr = " ";
htmlWrite.Write(scr);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write(" ");
HttpContext.Current.Response.End();
}
You can pass the control to be printed as a parameter of PrintWebControl(Control ctrl) function.
Happy coding Shout it kick it on DotNetKicks.com
0
Posted on 9:36 PM by prajeesh and filed under ,

If you have a page that takes long time to display it is a good idea to display a "wait page loads" image. This post show you how to implement this in your webpage.

To implement this you will need to:

1. Every time your page loads a "init()" function will load.

<body onLoad="init()">

2. Define a div named "loading" right after <body> section.

<div id="loading" style="position:absolute; width:100%; text-align:center; top:300px;">


<img src="loading.gif" border=0></div>

The loading.gif image should be an animated gif that suggests that the page is still loading.

3. Place this javascript code right after you define the div.

<script>
var ld=(document.all);
var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;
if (ns4)
ld=document.loading;
else if (ns6)
ld=document.getElementById("loading").style;
else if (ie4)
ld=document.all.loading.style;
function init()
{
if(ns4){ld.visibility="hidden";}
else if (ns6ie4) ld.display="none";
}
</script>
Shout it kick it on DotNetKicks.com
4
Posted on 9:34 AM by prajeesh and filed under , ,

In some websites we can see buttons like A A A to increse font size of your website for giving readability to disabled people also,we can do it many ways,one option is to dynamically change css or using javascript.here is a simple method using javascript to do this.


Step 1: Add the following Javascript in the insde the <HEAD> tag of your HTML.


<script language="JavaScript" type="text/javascript">


function changeFontSize(inc)


{


var p = document.getElementsByTagName('p');


for(n=0; n<p.length; n++) {


if(p[n].style.fontSize) {


var size = parseInt(p[n].style.fontSize.replace("px", ""));


} else {


var size = 12;


}


p[n].style.fontSize = size+inc + 'px';


}


}


</script>



Step 2: Insert the following HTML anywhere in your blog - you can customize the text or replace it with visual graphics (like the alphabet A - one small and the other one slightly large)

<a href="javascript:changeFontSize(1)">Increase Font Size</a>
<a href="javascript:changeFontSize(-1)">Decrease Font Size</a>

You can extend this to either all the HTML elements on your blog or limit it to only the text sections. The font size is specified in pixels.



Shout it kick it on DotNetKicks.com
2
Posted on 10:19 PM by prajeesh and filed under , ,
Hi friends, here is a simplest way of javascript client side validation for an asp.net registration for ,all you need to do is place this code inside script tag and change thename of Controls to your control names.

function validateregform()

{

if (document.getElementById("<%
=txtName.ClientID%>").value=="")
{
alert("Name Field can not be blank");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}

if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
{
alert("Email id can not be blank"); document.getElementById("<%=txtEmail.ClientID%>").focus();
return false;
}
var emailPat = /^(\".*\"[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}][A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById("<%=txtEmail.ClientID%>").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID%>").focus();
return false;
}

if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
{
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID%>").value="http://" document.getElementById("<%=txtWebURL.ClientID%>").focus(); return false;
}
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$" ;
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url); if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.ClientID%>").focus();
return false;
}

if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
alert("Zip Code is not valid");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
var digits="0123456789";
var temp;
for (var i=0;i<%=txtzip.clientid%>").value.length;i++)
{
temp=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);
if (digits.indexOf(temp)==-1) {
alert("Please enter correct zip code");
document.getElementById("<%=txtZIP.ClientID%>").focus(); return false;
}
} return true;
}

Here validateregform contains four functions to validate name,email,web url and zip code.

Shout it kick it on DotNetKicks.com