Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Display Current Time on the web page

Below example helps you to display current time on the web page. Textbox value will be updated with the current time every second.

Add following code to the page,
<script type="text/javascript">
    function DisplayTime() 
    {
        var currentDate = new Date();
        document.getElementById("<%= TextTime.ClientID %>").value = currentDate.toLocaleTimeString();
        window.setTimeout("DisplayTime()", 1000);
    }   
</script>

<script type="text/javascript">
    window.setTimeout("DisplayTime()", 1000);
</script>

<asp:TextBox ID="TextTime" runat="server"></asp:TextBox>

Javascript Alert message from code behind in ASP.NET

We all know that we can display javascript alert message from code behind in ASP.NET using Page.ClientScript.RegisterStartupScript.
Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript","alert('hello world!');");
However, above method can break under ASP.NET AJAX environment. For AJAX we need to use ScriptManager.RegisterStartupScript. Below code displays javascript alert message both in AJAX environment and on regular page as well.
ScriptManager.RegisterStartupScript(this,this.GetType(),"myscript","alert('hello world!');",true);
For more information on ClientScriptManager.RegisterStartupScript Method you can visit,

Open a new Window in JavaScript

How  to Open a new Window in Javascript/using Javascript is easy. Copy below code in your page.

If you want to open a new window on button clik then use below code,
<input type="button" value="Open New Window" onClick="OpenNewWindow('http://www.google.com')" />

If you want to open a new window using hyperlink then use below code,
<a href="javascript:OpenNewWindow('http://www.google.com/')">Open Google</a>

Copy below javascript code between your <head> and </head> tags on the page.
<script type="text/javascript">
        function OpenNewWindow(url)
        {
            newwindow = window.open(url, 'mywindow', 'width=600,height=700'); 
            newwindow.focus();
        }
</script>

Display Textbox count on a Label

This solution will help you use asp Text box and a Label to display the count of characters,
Add below code on your Default.aspx,
<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" 
     MaxLength="160" ></asp:TextBox>   
<asp:Label ID="lblCharacters" runat="server"></asp:Label>

Add below javascript code on your default.aspx,
<script language="javascript" type="text/javascript">
    function DisplayCount() 
    {
        var length = document.getElementById('<%=txtComments.ClientID%>').value.length;
        document.getElementById('<%=lblCharacters.ClientID%>').innerText = length;
    }
</script>

Add following code on your Page_load event,
txtComments.Attributes.Add("onkeypress", "javascript:return DisplayCount();");

After adding above code, you can see the count on the label when you type in the textbox.

Disable Right click on web page

How to disable right click on web page or If you want to protect your source code or many times programmers may need to disable right click on web page. You can copy below script code in between <head> and </head> tag.

<script type="text/javascript">
    var message="You Can't right click on this page!";
    function clickIE()
    {
        if (event.button==2)
        {
            alert(message);
            return false;
        }
    }

if (document.all&&!document.getElementById)
{
    document.onmousedown=clickIE;
}
document.oncontextmenu=new Function("alert(message);return false")
</script>