Text slideshow in javascript

In this javascript tutorial we will learn how to develop text slideshow in javascript. Sometimes it become a necessary requirement to develop a text slideshow in the website so to avoid use of Flash because flash makes the webpage heavier and sometimes reader of the website don’t have any flash reader installed in web browser so its better to use javascript to develop the text slideshow.

Let’s have a look over code to understand how to implement text slideshow in javascript.

<html>

<head>

<title>Text Slideshow in javascript</title>

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

var slideArray = new Array()

slideArray[0]= "Here comes the <strong>First Slide</strong>.<br> Some of text goes here ";

slideArray[1]= "<strong>Second Slide</strong>.<br> Some of text goes here ";

function firstSlide()

{

document.getElementById('div_display').innerHTML=slideArray[0];

setTimeout("secondSlide()",10000);

}

function secondSlide()

{

document.getElementById('div_display').innerHTML=slideArray[1];

setTimeout("firstSlide()",10000);

}

</script>

</head>

<body>

<table width="100%" cellpadding="2" cellspacing="2">

<tr>

<td>

<div id="div_display"><script type="text/javascript" language="javascript">firstSlide();</script></div>

</td>

</tr>

</table>

</body>

</html>

Now it’s turn to understand the code.

First of all I have made an array and give it a name slideArray and put two elements in this array. Then I made a javascript function to display first array element, I give firstSlide() name to that javascript function. In this function I am assigning the first array element to the div so that it can be display in the div then I use built-in javascript function setTimeout() function that will redirect focus to the secondSlide() function so that second slide can also be displayed after ten seconds. Yeah you are right, 10000 means ten seconds delay.

Now let’s suppose a case in which you have five slides to display and you want to display them randomly. Let’s look on how to do this

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

var slideArray = new Array()

slideArray[0]= "Here comes the <strong>First Slide</strong>.<br> Some of text goes here ";

slideArray[1]= "<strong>Second Slide</strong>.<br> Some of text goes here";

slideArray[2]= "<strong>Third Slide</strong>.<br> Some of text goes here";

slideArray[3]= "<strong>Fourth Slide</strong>.<br> Some of text goes here";

slideArray[4]= "<strong>Fifth and Final Slide</strong>.<br> Some of text goes here ";

function textSlideShow()

{

var total_slides;

total_slides=Math.floor(Math.random()*slideArray.length)

document.getElementById('div_display').innerHTML=slideArray[total_slides];

setTimeout("textSlideShow()",10000);

}

</script>

And then call this javascript function inside the div element in which you want to display text slideshow like below.

<div id="div_display"><script type="text/javascript" language="javascript"> textSlideShow();</script></div>

Every time you will get random slide.

Now let’s suppose a case in which you want to show text coming from database in slideshow rather than static text.

First of all use any web programming language such as php, asp.net, asp to get data from database and then assign that data to different textboxes and hide these textboxes then do this to get data from textbox and use that data as a text in slideshow.

<html>

<head>

<title>Text Slideshow in javascript</title>

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

var slideArray = new Array()

var myName,myAge;

function firstSlide()

{

myName=document.getElementById('txt_Name').value;

slideArray[0]=myName;

document.getElementById('div_display').innerHTML=slideArray[0];

setTimeout("secondSlide()",10000);

}

function secondSlide()

{

myAge =document.getElementById('txt_Age').value;

slideArray[1]=myAge;

document.getElementById('div_display').innerHTML=slideArray[1];

setTimeout("firstSlide()",10000);

}

</script>

</head>

<body>

<table width="100%" cellpadding="2" cellspacing="2">

<form name="form1">

<tr>

<td>

<input type="text" id="txt_Name" style="display:none;" value="Hello"><br>

<input type="text" id="txt_Age" style="display:none;" value="Hello2">

<div id="div_display"><script type="text/javascript" language="javascript">firstSlide();</script></div>

</td>

</tr>

</form>

</table>

</body>

</html>

So these are the different methods that you can use to develop text slideshow in javascript. I hope you like this article.

Happy Coding…!

Read more...

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>)

Once I was working in master page and wanted to call the javascript function that contains Code Blocks. Every time I got this error The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>). I was feeling headache to solve that error then one of my friend told me solution to get rid of this error and then every thing gone fine.

Actually I had an asp textbox control and an aspx button control in my masterpage, I wanted to get the value of <asp:TextBox id="txt_Name" runat="server"> in my javascript function but every time I got that error then I cut my javascript function from <head> </head> and put it into <form name="form1" runat="server"> of master page and then every thing worked fine.

You just have to put your javascript function in the form tag of master page

Here’s my javascript function

<form id="form1" runat="server">
<script type="text/javascript" language="javascript">

function FormValidation()
{
var my_Var;
my_Var=document.getElementById('<%=txt_Name.ClientID %>').value;

if(my_Var=="")
{
alert("Please enter the name");
return false;
}
else
{
return true;
}

}
</script>
</form>

Note:- The form attribute must contain runat="server" attribute.

So this is the way to avoid The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>) error.

Happy Coding!

Read more...

How to find the div in javascript

In this javascript tutorial we will learn how to find the div that we want to find. Once during coding it became a necessary requirement for me to find a specific div, I use below mentioned code to find that div, actually I want to execute another javascript function if specific div found else nothing should be executed.

So let's have a look on how to find the div in javascript.

<script language="javascript" type="text/javascript">
function findingDiv()
{
Div_Collection = document.getElementsByTagName("div");
for (i=0;i<Div_Collection.length;i++)

{
if(Div_Collection[i].getAttribute("id") == "my_div")
{

Myfunc();

}
else
{
}
}
}
</script>

Now it’s up to you and up to condition where u will call this function. In this function first of all I get all div elements and store these div elements in a Div_Collection variable that is actually playing role as an array. It’s necessary that there must be some unique thing in the div that you are going to be find, I assigned a unique ID to the div that I am going to find. In my case that div ID is my_div. After that I apply for loop on the array and then search for that specific div by ID. If that div found then I am executing another function else nothing.

So this is the way to find the div in javascript. I hope you like this javascript tutorial.


Note:- You can also find elements other than div using this javascript technique Read more...

Counting words in a textarea using javascript

In this javascript tutorial we will learn how to count the number of words entered in a textarea using javascript. Let's have a look over below mentioned code that demonstrates how to count the words entered in a textarea using javascript.

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

function counting_words(txt_Name)
{

var number_of_words;
number_of_words=txt_Name.value.split(" ");

alert("Textbox has "+number_of_words.length+" words");

return false;

}

Now here's the code that will made textarea in your webpage.

<form name="form1">
<textarea name="details"> </textarea>
<input type="submit" value="Check" onclick="return counting_words(document.form1.details)"></form>

Now when you will entered the words in textarea and then press submit button then an alert will come that will show you how many words you have entered in the textrea.

So this is the way to count words in a textarea using javascript.
Read more...

Checking of empty and null string in asp.net using C#

In this tutorial we will learn how to check the empty string and null string in asp.net using C#. But before moving forward let’s understand what is the difference between null string and empty string.

Null String:-
A string is said to be a Null String if it hasn’t been assigned any value yet.

Empty String:-
A string is said to be an Empty String that has been assigned an empty value.

Empty and null string is different and we must differentiate between them.

You may get error if you want to check the certain property of the string i.e; length of the string then you may get error if that string is NULL because we can’t get the length of Null String. So it’s better to check the string before applying any function over it. In this tutorial we will learn how to detect empty string as well as null string in asp.net using C#.

Now let’s consider we have a textbox in .aspx web page suppose we have
<asp:TextBox ID="txt_Name" runat="server"></asp:TextBox> in our .aspx webpage now on Button’s OnClick event we are going to check whether txt_Name contains empty/Null value or not.

Methods to check the empty string:-

In this section we will learn different techniques to detect the empty string in asp.net using c#

Check_empty_null_string.aspx.cs

Method 1
if (txt_Name.Text.ToString().Equals(""))
{
lbl_message.Text="Contains Empty value";
}

Method 2
if (txt_Name.Text.ToString().Length == 0)
{
lbl_message.Text="Contains Empty value";
}

Method 3
if (txt_Name.Text.ToString().Equals(""))
{
lbl_message.Text="Contains Empty value";
}

Methods to check the null string:-

if (txt_Name.Text.ToString() == null)
{
lbl_message.Text="Contains Null value/ Null string";
}

Sometimes you may get error like this Object reference not set to an instance of an object when you intend to get any property of the string but that string is null, so it’s better to check the empty string as well as null string in one statement to make code as short as you can ant to make application more free.

Methods to check the null string and empty string in one statement

Method1
if (string.IsNullOrEmpty(txt_Name.Text.ToString()))
{
lbl_message.Text="Contains Empty value or Null Value";

}

Method2
if(txt_Name.Text.ToString()!=null && txt_Name.Text.ToString()!=string.Empty)
{
lbl_message.Text="String is neither empty nor null";
}

You can also check the existence of empty string or null string by applying the above mentioned methods over the values coming from database.

So this is the way to check and detect the empty and null string in asp.net using c#.



Read more...