Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Dotnet like Trim method in JavaScript to Trim any string, char or blank spaces

We have already discussed, How to create own extension method in javascript. By using that extension methodology, we will create our own dotnet like trim method, which will able to trim blank spaces, character, string etc.

In this you will able to use the trim methods in your case as as
1. LTrim
       Trim Blank Spaces: var trimmedText = "Akash    ".LTrim();  
                  Output: Akash;
       Trim Some String: var trimmedText = "AkashSS".LTrim("S");
                Output: Akash
2. RTrim

       Trim Blank Spaces: var trimmedText = "    Akash".RTrim();  
                  Output: Akash;
       Trim Some String: var trimmedText = "SSAkash".RTrim("S");
                Output: Akash


3. Trim

       Trim Blank Spaces: var trimmedText = "     Akash    ".Trim();  
                  Output: Akash;
       Trim Some String: var trimmedText = "SSSAkashSS".Trim("S");
                Output: Akash


Code

How to insert Input Elements dynamically in your blogspot posts

By using the following steps you can insert input elements in your blogspot post dynamically.

Step 1: Start Writting New Post by clicking on the "Create New Post" button






Step 2: Goto HTML view of your post.






Step 3: Add a div where the Input elements are to be added, and give Id to the div. As in my example I have given the Id of the div as "demoDiv". Add another div which will be used to raise click event to add the button. In this demo I have added the div as "insertElementDemo








Secure waiting Timer with JQuery

Many time in our web-site, we used to put a waiting timer for user to wait to take any further action.This timer can be written in JavaScript or in JQuery. The drawback of the writing the timer in JavaScript is that it is not secure. Anyone can override the timer by executing the javascript from browser's address bar. So to overcome this situation we need to synchronize the timer with the server.

Consider the code for the JavaScript Timer written below


<html>
<head>
<script type="text/javascript">
    var waitTime = 90;
    function startJSTimer() {
        try {
          setInterval(function () {
          if(waitTime>0){
             waitTime = waitTime - 1;
             document.getElementById("timer_count_js").innerHTML = waitTime;
           }
           else{
               return false;
             }
           }, 1000);
        }
        catch (ex) {
            alert('Error');
        }
    }
</script>
</head>
<body>
<div id="timer_blockjs" style="border-color: Black;" title="JavaScript Timer">
<div class="content">
        <div>
seconds</div>
<div id="Div2">
            <span id="timer_count_js">90</span>
        </div>
<div class="text">
<a href="#" id="start_timer_js" onclick="startJSTimer();">Start timer</a></div>
</div>
</div>
</body></html>

In the above html code the timer is written in Javascript which can easily be changed by executing the following javascript from Address Bar..

javascript:function A(){waitTime=1;} A();


How to create Extension methods for predefined datatype in JavaScript

In Javascript one can create extension(for eg: for string it can be obj.Trim(), obj.LTrim(), obj.RTrim etc) methods for predefined data types. These kind of methods can be created by using prototype property of datatypes.
Here In below code I am showing Extension methods for String DataType.


<script type="text/javascript" language="javascript">
        String.prototype.LCase = function () { return this.toLowerCase(); }
        String.prototype.Trim = function() { return [Some PROCESSED VALUE];}
    </script>

Now as we have written Two extension methods for string data types and suppose we have string variable called str  then we can use these methods like str.LCase(), str.Trim()