Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Disable textbox cut copy paste in asp.net and asp.net mvc using javascript,jquery

Udemy
In some scenarios, we don't want user to be able to cut or copy the value of text box and we also want to restrict user to not be able to paste value from clipboard in text box.


User have couple of options, he can use short cut keys , for example Ctrl + C for copying and Ctrl + V , or user can right click inside the control and can use use context menu buttons which are mostly Cut, Copy and Paste available for doing desired operation on control.


One way is to write java-script events  for cut,copy and paste so that whenever user do one of these operations on control we handle them to override the default implementation of these commands.



In asp.net mvc we can use the htmlAttributes parameter of Html Helper method do it.

Disabling Using  JavaScript:


Here is an example code using javacript:

@Html.TextBoxFor(model => model.Question, 
          new {
                       @class="form-control",
                       oncut="return false"
                       oncopy="return false", 
                       onpaste="return false"
                     }
                 )

Here is the Live DEMO Fiddle of it.


Disabling Using JQuery:


We can do the same using jquery as well this way:

@Html.TextBoxFor(model => model.Question, 
          new {
                       @class="form-control",
                       id="txtQuestion"
                       oncut="return false"
                       oncopy="return false", 
                       onpaste="return false"
                     }
                 )

and in jquery :

$("#txtQuestion").on("cut copy paste",function(e) {

   e.preventDefault();
    // it will cause to stop default behaviour ofcommand
   // or we can use return false as well, as we did in js

}

 You can do it same way in asp.net web forms as well, for example:

<asp:TextBox ID="txtQuestion" 
             runat="server"
             CssClass="form-control"
             oncut="return false"
             oncopy="return false" 
             onpaste="return false">
</asp:TextBox>



Javascript deep Copy Array (Clone Javascript array without reference)

Udemy
Today i saw a SO question, in which the OP was asking about how to clone a javascript array in a variable so that it does not have reference tot the original array.


Here is the scenario:


var fruit = function (name){
    this.name = name;
}
var fruits = [];
fruits.push(new fruit('apple'));
fruits.push(new fruit('banana'));
fruits.push(new fruit('organge'));

var fruits2 = fruits;
fruits2.length = 0;

Now in the above when we assign fruits to fruits2, fruits2 actually has the reference of fruits, after that line if you see we are emptying the fruits2 array by setting its lenght to 0. But it will also make fruits array empty as well.


See here in the JSFiddle DEMO:

Now we have to make a way to do deep clone of array so that it not refers to original array, we can do it using slice() function of javascript.

Summary of slice():

The slice() method returns a shallow copy of a portion of an array into a new array object.

Description:

slice does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:
  • For object references (and not the actual object), slice copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
  • For strings and numbers (not String and Number objects), slice copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array.

EXAMPLE:




// Our good friend the citrus from fruits example
var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
var citrus = fruits.slice(1, 3);

// puts --> ['Orange','Lemon']

When we write slice(0)  , it returns the complete array shallow copy in return so our modified code will be:

 

var fruit = function (name){
    this.name = name;
}
var fruits = [];
fruits.push(new fruit('apple'));
fruits.push(new fruit('banana'));
fruits.push(new fruit('organge'));

var fruits2 = fruits.slice(0);
fruits2.length = 0;
console.log(fruits);

Here is the UPDATED FIDDLE  in which you can see it live working.



How to detect user reached bottom of page while scrolling

Udemy
Few days before i needed to implement on scroll paging as the facebook or linkedin is doing, instead of paging i thought to do infinite page scroll paging in my application so i decided to share the thing here as well.


Here is a sample code. Add this html to a empty html page:


<div style="height: 4000px">Scroll down!</div>

Here is the jquery code, but remeber to add jquery minified version in your page head as well like this:


<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>


Here is the jquery code to detect if user has reached page end while scrolling:


<script type="text/javascript">

        $(window).scroll(function () {
            if ($(window).scrollTop() + $(window).height() == $(document).height()) {
                alert("bottom!");
            }
        });

    
    </script>


here is the code to detect if user is near the bottom:


 $(window).scroll(function() {   
   if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
       alert("bottom!");
   }
});

Hope you got it. -:)