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

December 29, 2015

Simple Read more; Read less - Javascript


This tutorial is intended to be as short as possible since almost everything needed to get things done is clearly explained with the code.
The idea is to display certain amount of text extend of rendering the entire text on the webpage. Once the text exceed a certain limit, part of it with will be hidden with a link - Read more that will prompt your visitors to click to read more of the article. That's just it.
Source code:
Imagine your HTML looks like this;
18 When I say unto the wicked, Thou shalt surely die; and thou givest him 
<span id="restOfArticle" style="display:none">blah blahnot warning, nor 
speakest to warn the wicked from his wicked way, to save his life; the same 
wicked man shall die in his iniquity; but his blood will I require at thine hand. 
19 Yet if thou warn the wicked, and he turn not from his wickedness, nor 
from his wicked way, he shall die in his iniquity; but thou hast delivered thy soul. 
20 Again, When a righteous man doth turn from his righteousness, and commit 
iniquity, and I lay a stumblingblock before him, he shall die:because thou hast 
not given him warning, he shall die in his sin, and his righteousness which 
he hath done shall not be remembered; but his blood will I require at thine hand. 
21 Nevertheless if thou warn the righteous man, that the righteous sin not, 
and he doth not sin, he shall surely live, because he is warned; also thou 
hast delivered thy soul.
(Ezekiel 3:18-21)</span><a onclick="showMoreOrLess(this,'restOfArticle');">Read more</a>

and your Javascript file looks like this;
<script type="text/javascript">
function showMoreOrLess(thisObj,bonusContent){
    var caption = thisObj.innerHTML;
    //alert(caption);
    if ( caption == "Read more" ) {
        document.getElementById(bonusContent).style.display = "inline";
        thisObj.innerHTML = "Read less";
    } else {
        document.getElementById(bonusContent).style.display = "none";
        thisObj.innerHTML = "Read more";
    }
}
 </script>

With these two in place, you have your Read more; Read less in place. Examine the HTML section carefully. You will notice that it is divided into two parts. The part that first shows the text that you would want your page to display then the second part hides the text that prompt your visitor to click on Read more in order to access the second portion of the text.

December 23, 2015

Disable your visitor's keyboard using Javascript



Have you thought about the subject of this post? Am here to let you know that you can actually do that using Javascript just with a few lines of code. This thought came to me when I was working on an online payment project. The call for this came in when I considered providing just a few characters to the user; therefore, disabling other characters that could be used for malicious purpose. For me, I had to develop an on-web keyboard that provided the characters I would want him/her to enter.
Here is the code that can manifest that thought of yours.

<script>
(document.onkeydown = function (e) {
  e.preventDefault();  
}
</script>


The code above disables the entire keyboard. Your visitor will not be able to use it at all. Document refers to the webpage that this script is used on. It calls the Javascript event handler "onkeydown". We then use the object of that event handler to call the default behavior. The one below disables all the keys except the ENTER or CARRIAGE RETURN button.

<script>
document.onkeydown = function (e) {
  var key = e.charCode || e.keyCode;
  if (key == 13) { 
    // enter key do nothing
  } else {
    e.preventDefault();
  }      
}
</script>


We used the charCode or keyCode to permit the ENTER key on the webpage. The ASCII code for ENTER button is 13. So therefore, it is used in the if conditional statement to allow the key to be used. To help us exclude more keys instead of going through the stress of developing your own on-web keyboard, the image below contains all the ASCII code associated with each keyboard key. Find the code corresponding to the key you want to enable on the keyboard and use it in the if conditional statement like the one above. You can create an array that could hold all the keyCode or charCode and loop through them to get things done. This will save you multiple lines of code. Happy coding!

How to disable mouse right click on your webpage


Though this used to be a hot subject of discussion and received a lot of search queries early when Javascript came on the scene. I still consider it helpful and useful depending on what you intend to do at the moment. If you've worked on an Online Quiz project before, maybe your client must have raised something in this direction. Disabling candidates from trying to right click to maybe copy something on the webpage. Similarly, someone maybe a visitor to your site wants to copy your source code by trying to right click to inspect element or know the path of your file directory on your hosting server. As I pointed it out early, it all depends on what you intend to do at the moment. For the time being, let's see what we can use Javascript to achieve in this respect.

This Javascript can be on an external file or be embedded on the same page. The choice is yours. Paste this code your Javascriot section and make the necessary changes needed.
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
function disableclick(event)
{
  if(event.button==2)
   {
     alert(status);
     return false;    
   }
}
</script>

In addition to that code above, this should be pasted on the page you intend to disable mouse right click.
<body oncontextmenu="return false">
...
</body>

With this you are good to go.