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

How to redirect in javascript after some time delay

Redirection in javascript after some time delay
For this purpose there is a built-in javascript method setTimeout().
setTimeout() method is used to repeatedly call some function after a specified amount of time.

Syntax of setTimeout() Method
setTimeout("Javascript Statement",Milliseconds);

Note: - The setTimeout() is the method of the HTML DOM Window object.

setTimeout() method takes two parameters. The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('2 seconds!')" or a call to a function, like "helloMsg()".

The second parameter indicates how many milliseconds from now you want to execute the first parameter.

Note: - There are 1000 milliseconds in one second.

So lets have a look over the code

Redirection in javascript after some time delay

The above mentioned code will redirect user to the google.com after 5 seconds of clicking the button. When user will click the button, the function Delayer() will be get call and in this function I am calling Redirection() function after every 5 seconds. So when focus will move to the Redirection() function after every 5 seconds then the user will be immediately redirected to the google.com

Specify Multiple Actions from Single Form Using Javascript

yourpage.php


Specify Multiple Actions From Single Form

First Name
Last Name
Email
Username

Now lets understand.

First of all I have created a simple html form with two noticeable things. First noticeable thing is that there is no submit (type="submit") button in the form and secondly I have not set any action attribute of the form.

To post the data to two different web pages I have created two input buttons and using onClick event of these buttons I am setting the "action" of the form dynamically and then submit the form. So whenever user clicks the Button1 then the Page1.php will be called and whenever user clicks the Button2 then the Page2.php will be called.

How to write and execute the javascript code from c# code behind

string name;

string std_name;

if(name==std_name)

{

//Now here you want to execute your javascript code

}

Learn how to write and execute the javascript code from c# code behind


In my .aspx page I am using a and . When you will click the , focus will go to the server side then there I have written my code that will alert a message and display my name in the . Let’s have a look over how to do so

protected void submit_Click(object sender, EventArgs e)

{

string popupScript = "";

Page.RegisterStartupScript("Startup", popupScript);

}

Page.RegisterStartupScript is used for registering and executing the client side script. This method takes two parameters. First one is key of type System.String and it acts as a unique key that identifies a script block. Second parameter is script which type is also System.String, it is content of script that will be sent to the client, means it contains your javascript code.

Page.RegisterStartupScript automatically attach the script just before the closing tag of your aspx page object’s


element.

Page.RegisterStartupScript method uses a unique key to identify the script block; the script block does not have to be emitted to the output stream every time it is requested by a different server control instance.

Any script block with the same key parameter values is considered duplicate. You must remember to include HTML Comment Tags around your script so that it will not be rendered if the requesting browser does not support javascript.

Restrict special characters being entered in textbox using javascript

How to restrict special characters.

Now let's have a look over how to call this special characters restriction function in a textbox.

For aspx server side textbox control

For html textbox control

You can restrict any other character or number other than special characters too, just by simply add it in the list of characters to be restricted such as for example I want to restrict the character a too then the code will be

if (keychar == "'" || keychar == "`" || keychar == "a")

So this is the way to handle and restrict the special characters being entered in textbox using javascript.

How to disable the asp: textbox using javascript

In this tutorial you will learn how to disable the asp:Textbox using javascript. I will show you how to do so in just one and simple line of javascript code. Let's have a look over it.

disable the asp:Textbox using javascript



Using the OnClientClick event of asp:button i am calling the javascript function to disable the asp:textbox . So this is the way to disable the asp:textbox using javascript.

Set focus to table using javascript

How to set focus to table using javascript

Focus_table.aspx

Get Focus on Table

You have to give the id to the table. When you will run this code snippet then focus will be set to the desired table but this code snippet will not work in firefox.
Now to solve the browser compatability issue you have to put any form field in the table that you want to focus and then using javascript code you have to focus that form field and due to that, focus will be automatically set to that table. So let's have a look over this solution

Get Focus on Table

Now this code will run on almost all browsers but there is a drawback with it. The cursor will directly blink in the textbox and due to this you cannot go to previous page with the Backspace button of your keyboard. Also it is very awkward that cursor is blinking in textbox. But if you hide the textbox using css i.e; style="display:none;" and call the javascript function to focus the textbox then javascript function will throw an error.
Now to get rid of this issue, there is a solution and that solution is to use tag. Let's have a look over it.

Get Focus on Table

And this code snippet will work on all browsers and moreover you can go to previous page by using Backspace key on your keyboard. But I doesn’t recommend this method to set the focus to table/div using javascript. Why should we do extra things????? Let’s have a look over the solution that I strongly recommend, you don’t need to put any form field inside the table/div, and you don’t need to put any extra hyperlink tag near/inside the table/div. Just follow this code snippet.

Get Focus on Table

So this is the pretty line of javascript code that will solve your problem and set focus to the table. You can use these logic to set focus to div element as well.

How to use Redirection from iframe in asp.net

Redirection from iframe in asp.net

protected void btn_Submit_Click(object sender, EventArgs e)

{

//your code to verify username/password will come here

ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "if(top!=self) {top.location.href = 'next-page.aspx';}", true);

}

Note:-
In case if this code is not working properly in IE9 then you have to give the absolute path of targeting webpage such as

if(top!=self) {top.location.href = 'https://www.yourwebsite.com/next-page.aspx';}", true);

Using the server side onClick event of asp:button, we are redirecting user to another page but now page will not be open inside the iframe because the javascript code that we have written in the server side onClick event of asp:button will take care that user must be redirected to next page within parent window rather than IFrame

How to limit the text length in textbox using javascript

1) Page will show an error after getting respond from database because the column of the table that will store the data might be have width of 100 characters.

2) Incase user enters the script code of thousands of characters then your website may be down or can have other related problem


So bottom line is ALWAYS LIMIT THE TEXT LENGTH IN TEXTBOX. In this tutorial I am covering this topic using javascript the client side language but later on in this blog you will find how to achieve same thing using asp.net the server side because javascript might be disable in user’s/hacker’s system. I write user’s because I believe user can also harm your database unintentionally if you have not taken any precautions.

Now let's have a look over how to apply limit to the text length in textbox either it is Singleline or Multiline.

Limit the text length in textbox using javascript

</p> <p class="MsoNormal">Limit the text length in textbox using javascript</p> <p class="MsoNormal">




I could apply the text limit in textbox without using any single line code of javascript by using maxlength property of textbox like given below





The maxlength attribute of textbox works fine when textbox is in singleline mode but what about if textbox is in multiline mode? If textbox is multiline (i;e;

Format the currency using javascript | How to Format the currency in javascript

Format-currency-using-javascript.html

Format currency using javascript

At the top of the function, the comma character is assigned to variable delimiter. That's the comma between the quotation marks. If you want to use a different delimiter, replace the comma character with the character of your choice.


The function splits the amount on the basis of decimal point. The digits after the decimal point are stored in variable d.


The number in front of the decimal point is the entire amount, stored as an integer in variable i. If i is not a valid number, the function returns a null value.


Negative numbers are handled similar to the currency conversion function.


After then three digits are excluded from the end of the number and assigned to array variable a, so long as the number is longer than three digits. The number is then reformed using array a elements, with the delimiter digit inserted between each element.


Finally, the decimal portion is appended to the number, minus sign put in front, if appropriate, and the number returned.


Now formattedAmount variable contains the formatted currency amount.


Note: - This function can only accept amount that contains the decimal point like 1000.00, if you will provide amount 1000(No decimal point) to this function then it will not format the amount.


For those who wish to provide input of amount without any decimal point such as 1000 then they can refer to the below example in which I further customized the CommaFormatted() function according to our need.

How to format currency in javascript

Now let's have a look over the second example that can round numbers to the nearest hundredth, with two digits after the decimal point. It will help you when displaying the total price of the orders.

</p> <p class="MsoNormal">Format currency in javascript</p> <p class="MsoNormal">

The function CurrencyFormatted(amount) stores the amount in variable i as a floating decimal point number. It also uses parseFloat() in case the function is sent a string representing a number instead of number itself.


The next line checks the existence of valid number. If user sends function the value "hello world" would result in a non-number and as a response the function converts the non-number into "0.00", you can also prompt a message to the user to enter valid number.


After that, the function CurrencyFormatted(amount) determines whether the number is positive or negative, converts it to positive if necessary, and then rounds the number to the nearest hundredths.


Finally, it makes sure there are two digits following the decimal point and prepends the "-" character if it started as a negative number, and returns the result.


Now if user will provide the amount 9999.999 as an input then you will get 10,000.00 as an output.


So these are the examples to format the currency using javascript. I love your feedback.

How to Format currency using javascript

1) Insert commas between every third digit from right to left for numbers 1000 and greater. It will make numbers easier to read. The number 9,999,999.00 is easy to read as compare to 9999999.00


Input:-
1000.00


Output:-
1,000.00


2) Round numbers to the nearest hundredth, with two digits after the decimal point. It will help you when displaying the total price of the orders.


Input:-
9999.999


Output:-
10,000.00

Let's have a look over the first example that will insert commas between every third digit from right to left for numbers 1000 and greater.

How to avoid jquery conflict with other javascript libraries

Avoid jquery conflict with other javascript libraries

Use of .noConflict() method of jQuery

It will revert $ back to its original library.


Additionally, there is another option for you. If you want to make sure that jQuery won't conflict with other javascript libraries - but you want the benefit of a short name, you can do something like i have done below:

You can define anything as your own alternate name (e.g. hello, pakistan, cricket, jq).


Finally, if you don't want to define alternative to the jQuery name (you deadly want to use $ and don't care about using another library's $ method), then you have another solution. This is most frequently used in such case when you still want the benefits of really short jQuery code, but you don't want to cause conflicts with other javascript libraries.

This is probably the perfect solution for most of your code, considering that there'll be less code that you'll have to change for getting complete compatibility of jquery with other javascript libraries. So these are the different techniques to avoid jquery conflict with other javascript libraries.

How to create div element dynamically by using javascript | Learn javascript

Create div element dynamically using javascript

And if you want to want to create div element inside any other html element of page body then nothing to worry. Let's suppose we have a div in our page which id is testDiv like given below



and you want to create div inside this testDiv. Just you have to change the last line of CreateDivElement() function from


document.body.appendChild(divElement);
to
document.getElementById('testDiv').appendChild(divElement);

How to get page name from url in javascript

Get page name from url in javascript.

So this is the way to get page name from url in javascript

Fix png transparency problem in IE 5.5 and 6 | How Fix png transparency

How to fix png transparency problem in IE 5.5 and 6

You just have to insert the following code in the head tag of your web page.


So that's it. This is the way to fix the png transparency problem in IE 5.5 and 6.

This code will only execute if user's browser will be IE 6 or prior than IE 6. So there will be no extra load in your web browser if it is firefox, google chrome, opera, safari or internet explorer greater than 6.

Get missing leading zero while conversion of number to string in javascript

Today while developing the countdown module, i got a requirement to show the days value which was a number like 01,02,03,04.... up to 30, in two different spans in such a way that the leading 0 of 01 value should be shown in first span and then 1 in second span, likewise if we have 21 as a value then 2 will be shown in first span and 1 will be shown in second span. I Just want to clear your confusion, in my example the value of days variable change dynamically when countdown start. I was very much amazed when i saw the conversion of number 01 to string resulting in only 1, the conversion of number 02 to string resulting in only 2, and then suddenly i thought where is leading zero???

So to solve this problem and get missing leading zero, you have to follow the example given below

The variable days store integer(Number, Numeric) value that comes dynamically, that's why the value is not surrounded by any " ". If days="01" then there was no problem by getting 01 as a 0 and 1 because any thing inside the double quotes is called string and we can get substring easily from string.

How to detect browser in javascript

Note:-
The version detect doesn't work in Safari and OmniWeb because these browsers donot include appropriate version information in their identity string.

How to use Countdown timer in javascript

  1. Cross browser compatibility.
  2. Doesn’t get unnecessary calls.
  3. Easy to implement.
  4. Work smoothly and perfectly.

Just you have to do is to set a specific target date in the future, get the current date and then calculate the remaining time between them.

So let’s have a look over the example given below

Countdown timer in javascript

Countdown.html

And here's the html code of this example that you will copy and paste in your web page

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Days

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Hrs

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Mins

z-index: 1000; bottom: 0px; left: 0px; font-size: 20px;font-family:verdana;">Secs

z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;"

id="days1">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;"

id="hrs2">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

position: absolute; z-index: 1000; bottom: 0px; left: 0px; font-size: 50px;

">

This countdown timer function will give you countdown between current date, that it will pick from your system date, and the date mentioned in calling of countdown timer function which is countdown(2011, 12, 25);

You just have to replace this date with the date you desire for countdown time. I have used jquery’s $(document).ready(function ()) event because I want to execute this function when every DOM is loaded. But if you don’t want to use jquery and its ready event then exclude the jquery file and its code from the example and copy/paste following code just before tag

This technique is equivalent to jquery’s document.ready event.

I have used two divs for showing days, two divs for showing hours, two divs for showing minutes and again two divs for showing seconds. For example if remaining days are 14 then 1 will be store in days1 div and 4 will be stored in days2 div. You can use one div for showing remaining days, hours, minutes and seconds.

Moreover, I have applied all the checks in javascript code so that function will not get any extra call if countdown is finished, I have seen lot of code in internet that work perfectly but when countdown is finished but its code is still in web page then the countdown function gets extra and totally unnecessary calls but in my example I have taken care of this issue and avoid extra calls to the countdown timer function.