Sunday, April 17, 2016

How to prevent F12 in browser

Introduction:

Opening the console log of browser and change element value of a web page is like a headache for the developer. To prevent this situation here is the remedy.

Description:

So how to stop this kind of situation, you could get a message while user is opening the console log and take necessary step or you could prevent user from opening that. We will discuss both the situation in this article one by one.

Getting noticed while opening the console of the browser:

Create a normal HTML page and and add a div to show the status of the development tool box. And write down the code.



<body>
    status: <div id="status"></div>
</body>
 
After that write down the code within the script tag under body section.



<script>
    var checkStatus;

    var element = new Image();
    element.__defineGetter__('id', function () {
        checkStatus = 'on';
    });

    setInterval(function () {
        checkStatus = 'off';
        console.log(element);
        console.clear();
        document.querySelector('#status').innerHTML = checkStatus;
    }, 1000)
</script>


 Output:



Prevent Clicking F12 and Mouse Right Click:

This trick is not like showing status. By showing status you can do all your customization in your code but here in this example of coding user will not be able to click F12 and mouse right click. Lets see how.

Before starting add JQuery into your page. To add it add the bellow script to the top of all the JQuery code.



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
 
After that within a script tag write down the  bellow code.



<script>
    $(document).keydown(function (event) {
        if (event.keyCode == 123) {
            return false;
        }
        else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) {
            return false;
        }
    });

    $(document).on("contextmenu", function (e) {
        e.preventDefault();
    });
</script>


As you can see we have used the Key code to detect the clicking of buttons and mouse. Here 123 stands for F12 and 73 for I key. To know more visit https://msdn.microsoft.com/en-us/library/aa243025(v=vs.60).aspx.

Run both the examples and enjoy.




 

Friday, October 23, 2015

Difference between Response.Write and Response.WriteLine

Introduction:

In this post we will discuss about the difference between Response.Write and Response.WriteLine in C#. These are the methods we used to show the output in console application. Both these methods come from class Console under namespace System. Behavior of both these are as same as except only one thing.

So what is the difference then?

The only difference is the position of the cursor. In the Write() method the after the output has been printed the cursor remains in the same line, But in case of WriteLine() the cursor shifted to the next line, that means you don't need to worry about the next line printing in WriteLine(). You have to use a new line after using Write() method if you want to continue with the next line. To do so you can use System.Environment.NewLine.

Using Console.Write():

public void Method2()
{
    // Keep the cursor in that line only.
    Console.Write("Hi this is Console.Write. ");

    // Keep the cursor on the next line.
    Console.Write("Hi this is Console.Write. "+ System.Environment.NewLine);
}

Output:


Using Console.WriteLine():

public void Method3()
{
    // Keep the cursor on the next line.
    Console.WriteLine("Hi this is Console.WriteLine. ");
}

Output:


Check the cursor position in both of the output. In the first image the cursor is in the same line and in the second output the cursor is in the next line.


Follow by Email

Popular Posts

Pageviews