Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Getting Started (Javascript Tutorial)

কেন জাভাস্ক্রিপ্ট ?

জাভাস্ক্রিপ্ট কম্পিউটারেভাষার ক্ষেত্রে একটি শক্তিশালী, জটিল এবং খুব ভাল বুঝতে না পারা ভাষা । তবে, এর অন্তর্নিহিত শক্তি ইন্টারেক্টিভ ওয়েব পেজ এবং অ্যাপ্লিকেশন বিকাশ করার ক্ষমতা যোগায়, ব্যবহারকারীদের দক্ষতার সঙ্গে ফলাফল প্রবেশ করাতে এবং সেই ওয়েবপেজ এবং অ্যাপ্লিকেশান এর ফলাফল বাস্তবমুখী করে প্রকাশ করতে সাহায্য করে।

জাভাস্ক্রিপ্টের প্রাথমিক সুবিধা, যা ECMAScript হিসাবে পরিচিত, ওয়েব ব্রাউজারের চারপাশে কেন্দ্রীভূত হয়, এইভাবে ব্রাউজার দ্বারা সমর্থিত সমস্ত প্ল্যাটফর্মের উপর একই ফলাফল উৎপাদন করার ক্ষমতা থাকে এই পেজের উদাহরনগুলো, Google Maps-এমত ওয়েবসাইট গুলো, লিনাক্স, উইন্ডোজ এবং ম্যাক অপারেটিং সিস্টেমে চলে।অনেক জাভাস্ক্রিপ্ট লাইব্রেরির সাম্প্রতিক বৃদ্ধির কারণে একটি ডকুমেন্ট নেভিগেট করা, DOM উপাদান নির্বাচন করা, অ্যানিমেশন তৈরি করা, ঘটনা হ্যান্ডেল করা এবং Ajax অ্যাপ্লিকেশান তৈরি করা সহজ হয়।বিভিন্ন মালিকানা স্বার্থ দ্বারা চিহ্নিত অন্যান্য প্রযুক্তির চারপাশে প্রতারণা থেকে পৃথক, জাভাস্ক্রিপ্ট সত্যিই একমাত্র ক্রস প্ল্যাটফর্ম, ব্যবহারকারীর প্রতি মনোনিবেশিত ভাষা যা বিনামূল্য এবং সর্বজনগৃহীত।

ইতোমধ্যে আপনার কি জানা থাকা প্রয়োজন

প্রোগ্রামিং শুরু করার জন্য জাভাস্ক্রিপ্ট খুবই সহজ একটি ভাষা। আপনার যা দরকার তা হল একটি টেক্সট এডিটর ও একটি ওয়েব ব্রাউজার।

জাভাস্ক্রিপ্টের সাথে সংযুক্ত হতে পারে এবং বিকাশিত হতে পারে এমন অনেক প্রযুক্তি আছে যা ডকুমেন্টটির বাইরে অবস্থান করে। আপনার প্রথম দিনে গুগল ম্যাপের মতো একটি সম্পূর্ণ অ্যাপ্লিকেশান তৈরির কথা ভাববেন না!

চলুন শুরু করি

জাভাস্ক্রিপ্ট দিয়ে শুরু করা খুব সহজ। আপনার জটিল ডেভেলপমেন্ট প্রোগ্রাম ইনস্টল করা থাকতে হবে না। আপনাকে একটি শেল ব্যবহার করা জানতে হবে না, প্রোগ্রাম তৈরি করা বা একটি কম্পাইলার ব্যবহার করাও জানতে হবে না। আপনার ওয়েব ব্রাউজার দ্বারা জাভাস্ক্রিপ্ট ভাষান্তরিত করা হয়। আপনাকে যা করতে হবে তা হল একটি টেক্সট ফাইল হিসাবে আপনার প্রোগ্রাম সংরক্ষণ করা এবং ব্রাউজার দ্বারা তা ওপেন করা। এই যা!

কম্পিউটারের ভাষার সাথে পরিচিত হওয়ার জন্য জাভাস্ক্রিপ্ট একটি মহান ভাষা। এটি নতুন ছাত্রদের মধ্যে তাৎক্ষনিক প্রতিক্রিয়া সৃষ্টি করতে এবং বাস্তব জীবনে প্রয়োজনীয় নতুন টুলের সাথে পরিচিত হতে শিখায়। এটি সি, সি++ এবং জাভার সম্পূর্ণ বিপরীত যেগুলো মূলত কেবল উৎসর্গকৃত সফটওয়্যার ডেভেলপারদের জন্য প্রয়োজনীয়।

ব্রাউজার সামঞ্জস্যের সমস্যা

বিভিন্ন ব্রাউজারে প্রাপ্ত কার্যকারিতা মধ্যে তারতম্য আছে।মোজিলা ফায়ারফক্স, মাইক্রোসফট ইন্টারনেট এক্সপ্লোরার, অ্যাপেল সাফারি, অপেরার আচরণে পার্থক্য দেখা যায়। আমরা documenting these variations এ তা ব্যাখ্যা করেছি। আপনি সহজলভ্য বিভিন্ন ক্রস প্ল্যাটফর্ম জাভাস্ক্রিপ্ট API গুলো ব্যবহারের মাধ্যমে এই সমস্যা গুলো প্রশমিত করতে পারেন। এই API গুলি সাধারণ বৈশিষ্ট্য প্রদান করে এবং আপনার কাছ থেকে এই ব্রাউজারের পার্থক্য লুকায়.

উদাহরণ গুলো চেষ্টা করব কিভাবে

নীচের উদাহরণে কিছু নমুনা কোড আছে। এই উদাহরণ চেষ্টা করার অনেক উপায় আছে। যদি ইতিমধ্যেই আপনার নিজের ওয়েবসাইট থাকে তাহলে আপনি শুধু আপনার ওয়েবসাইটে নতুন ওয়েব পেজ হিসাবে এই উদাহরণ সংরক্ষণ করতে পারেন।

যদি আপনার নিজস্ব ওয়েবসাইট না থাকে, তাহলে আপনি আপনার কম্পিউটারে ফাইল হিসেবে এই উদাহরণ সংরক্ষণ করতে পারেন এবং এখন আপনি যে ওয়েব ব্রাউজার ব্যবহার করছেন তা দিয়ে সেগুলো ওপেন করতে পারেন। জাভাস্ক্রিপ্ট এই কারণে নতুন প্রোগ্রামারদের ব্যবহার করার জন্য একটি খুব সহজ ভাষা। আপনার একটি কম্পাইলার বা একটি ডেভেলপমেন্ট এনভায়রনমেন্ট প্রয়োজন হবে না। শুরু করার জন্য কেবল আপনাকে এবং আপনার ব্রাউজার কেই লাগবে!

Example: Catching a mouse click

The specifics of event handling (event types, handler registration, propagation, etc) are too extensive to be fully covered in this simple example. However this example cannot demonstrate catching a mouse click without delving a little into the JavaScript event system. Just keep in mind that this example will only graze the full details about JavaScript events and that if you wish to go beyond the basic capabilities described here to read more about the JavaScript event system.

'Mouse' events are a subset of the total events issued by a web browser in response to user actions. The following is a list of the the events emitted in response to a user's mouse action:

  • Click - issued when a user clicks the mouse
  • DblClick - issued when a user double-clicks the mouse
  • MouseDown - issued when a user depresses a mouse button (the first half of a click)
  • MouseUp - issued when a user releases a mouse button (the second half of a click)
  • MouseOut - issued when the mouse pointer leaves the graphical bounds of the object
  • MouseOver - issued when the mouse pointer enters the graphical bounds of the object
  • MouseMove - issued when the mouse pointer moves while within the graphical bounds of the object
  • ContextMenu - issued when the user clicks using the right mouse button

Note that in the latest versions of HTML, the inline event handlers, i.e. the ones added as tag attributes, are expected to be all lowercase and that event handlers in script are always all lowercase.

The simplest method for capturing these events, to register event handlers - using HTML - is to specify the individual events as attributes for your element. Example:

  <span onclick="alert('Hello World!');">Click Here</span>

The JavaScript code you wish to execute can be inlined as the attribute value or you can call a function which has been defined in a <script> block within the HTML page:

<script type="text/javascript">
  function clickHandler () {
     alert ("Hello, World!");
  }
</script>
<span onclick="clickHandler();">Click Here</span>

Additionally, the event object which is issued can be captured and referenced; providing the developer with access to specifics about the event such as which object received the event, the event's type, and which mouse button was clicked. Using the inline example again:

<script type="text/javascript">
  function clickHandler(event) {
    var eType = event.type;
    /* the following is for compatability */
    /* Moz populates the target property of the event object */
    /* IE populates the srcElement property */
    var eTarget = event.target || event.srcElement;

    alert( "Captured Event (type=" + eType + ", target=" + eTarget );
  }
</script>
<span onclick="clickHandler(event);">Click Here</span>

In addition to registering to receive events in your HTML you can likewise set the same attributes of any HTMLElement objects generated by your JavaScript. The example below instantiates a span object, appends it to the page body, and registers the span object to recieve mouse-over, mouse-out, mouse-down, and mouse-up events.

<body></body>
<script type="text/javascript">
  function mouseeventHandler(event) {
    /* The following is for compatability */
    /* IE does NOT by default pass the event object */
    /* obtain a ref to the event if one was not given */
    if (!event) event = window.event;

    /* obtain event type and target as earlier */
    var eType = event.type;
    var eTarget = event.target || event.srcElement;
    alert(eType +' event on element with id: '+ eTarget.id);
  }

 function onloadHandler () {
   /* obtain a ref to the 'body' element of the page */
   var body = document.body;
   /* create a span element to be clicked */
   var span = document.createElement('span');
   span.id = 'ExampleSpan';
   span.appendChild(document.createTextNode ('Click Here!'));

   /* register the span object to receive specific mouse events - 
      notice the lowercase of the events but the free choice in the names of the handlers you replace them with. 
   */
   span.onmousedown = mouseeventHandler;
   span.onmouseup = mouseeventHandler;
   span.onmouseover = mouseeventHandler;
   span.onmouseout = mouseeventHandler;

   /* display the span on the page */
   body.appendChild(span);   
}

window.onload = onloadHandler; // since we replace the handler, we do NOT have () after the function name
</script>

Example: Catching a keyboard event

Similar to the "Catching a mouse event" example above, catching a keyboard event relies on exploring the JavaScript event system. Keyboard events are fired whenever any key is used on the keyboard.

The list of available keyboard events emitted in response to a keyboard action is considerably smaller than those available for mouse:

  • KeyPress - issued when a key is depressed and released
  • KeyDown - issued when a key is depressed but hasn't yet been released
  • KeyUp - issued when a key is released
  • TextInput (available in Webkit browsers only at time of writing) - issued when text is input either by pasting, speaking or keyboard. This event will not be covered in this article.

In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property; never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.

The simplest method for capturing keyboard events is again to register event handlers within the HTML, specifying the individual events as attributes for your element. Example:

  <input type="text" onkeypress="alert ('Hello World!');" />

As with mouse events, the JavaScript code you wish to execute can be inlined as the attribute value or you can call a function which has been defined in a <script> block within the HTML page:

<script type="text/javascript">
  function keypressHandler() {
    alert ("Hello, World!");
  }
</script>

<input onkeypress="keypressHandler();" />

Capturing the event and referencing the target (i.e. the actual key that was pressed) is achieved in a similar way to mouse events:

<script type="text/javascript">
  function keypressHandler(evt) {
      var eType = evt.type; // Will return "keypress" as the event type
      /* here we again need to use a cross browser method
         mozilla based browsers return which and others keyCode.
         The Conditional operator or ternary is a good choice */
      var keyCode = evt.which?evt.which:evt.keyCode; 
      var eCode = 'keyCode is ' + keyCode;
      var eChar = 'charCode is ' + String.fromCharCode(keyCode); // or evt.charCode
      alert ("Captured Event (type=" + eType + ", key Unicode value=" + eCode + ", ASCII value=" + eChar + ")");
   }
</script>
<input onkeypress="keypressHandler(event);" />

Capturing any key event from the page can be done by registering the event at the document level and handling it in a function:

<script type="text/javascript">
  document.onkeypress = keypressHandler(event);
  document.onkeydown = keypressHandle(event);
  document.onkeyup =keypressHandle(event)
 
</script>

Here is a complete example that shows key event handling:

<!DOCTYPE html>
<html>
<head>
  <script>
    var metaChar = false;
    var exampleKey = 16;
    function keyEvent(event) {
      var key = event.keyCode || event.which; // alternative to ternary - if there is no keyCode, use which
      var keychar = String.fromCharCode(key);
      if (key==exampleKey) { metaChar = true; }
      if (key!=exampleKey) {
         if (metaChar) {
            alert("Combination of metaKey + " + keychar)
            metaChar = false;
         } else { alert("Key pressed " + key); }
      }
    }
    function metaKeyUp (event) {
      var key = event.keyCode || event.which;
      if (key==exampleKey) { metaChar = false; }
    }
  </script>
</head>
<body onkeydown="keyEvent(event)" onkeyup="metaKeyUp(event)">
    Try pressing any key!
</body>
</html>

Browser bugs and quirks

The two properties made available through the key events are keyCode and charCode. In simple terms, keyCode refers to the actual keyboard key that was pressed by the user, while charCode is intended to return that key's ASCII value. These two values may not necessarily be the same; for instance, a lower case 'a' and an upper case 'A' have the same keyCode, because the user presses the same key, but a different charCode because the resulting character is different.

The way in which browsers interpret the charCode is not a consistently-applied process. For example, Internet Explorer and Opera do not support charCode. However, they give the character information in keyCode, but only onkeypress. Onkeydown and -up keyCode contains key information. Firefox uses a different word, "which", to distinguish the character.

Refer to the Mozilla Documentation on Keyboard Events for a further treatment of keyboard events.

Draft
This page is not complete.

Example: Dragging images around

The following example allows moving the image of firefox around the page.

<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
img { position: absolute; }
</style>

<script type='text/javascript'>
window.onload = function() {

  movMeId=document.getElementById("ImgMov");
  movMeId.style.top = "80px";
  movMeId.style.left = "80px";

  document.onmousedown = coordinates;
  document.onmouseup=mouseup;

  function coordinates(e) {
    if (e == null) { e = window.event;}
    
    // e.srcElement holds the target element in IE, whereas e.target holds the target element in Firefox
    // Both properties return the HTML element the event took place on.

    var sender = (typeof( window.event ) != "undefined" ) ? e.srcElement : e.target;

    if (sender.id=="ImgMov") {
      mouseover = true;
      pleft = parseInt(movMeId.style.left);
      ptop = parseInt(movMeId.style.top);
      xcoor = e.clientX;
      ycoor = e.clientY;
      document.onmousemove=moveImage;
      return false;
    } else { 
        return false;
    }
  }

  function moveImage(e) {
    if (e == null) { e = window.event; }
    movMeId.style.left = pleft+e.clientX-xcoor+"px";
    movMeId.style.top = ptop+e.clientY-ycoor+"px";
    return false;
  }

  function mouseup(e) {
    document.onmousemove = null;
  }
}
</script>
</head>

<body>
  <img id="ImgMov" src="https://placehold.it/100x100&text=JS" width="64" height="64" />
  <p>Drag and drop around the image in this page.</p>
</body>

</html>

Example: Resizing things

Example of resizing an image. (PS: The actual image is not resized, but only the image's rendering is.)
  <!DOCTYPE html>
  <html>
    <head>
      <style>
        #resizeImage {
          margin-left: 100px;
        }
      </style>
      <script>
      window.onload = function() {

        var resizeId = document.getElementById("resizeImage");
        var resizeStartCoordsX,
            resizeStartCoordsY,
            resizeEndCoordsX,
            resizeEndCoordsY;

        var resizeEndCoords;
        var resizing = false;

        document.onmousedown = coordinatesMousedown;
        document.onmouseup = coordinatesMouseup;

        function coordinatesMousedown(e) {
          if (e == null) {
            e = window.event;
          }

          var element = (typeof( window.event ) != 'undefined' ) ? e.srcElement : e.target;

          if (element.id=="resizeImage") {
            resizing = true;
            resizeStartCoordsX = e.clientX;
            resizeStartCoordsY = e.clientY;
          }
          return false;
        }

        function coordinatesMouseup(e) {
          if (e == null) {
            e = window.event;
          }

          if (resizing === true) {
            var currentImageWidth = parseInt(resizeId.width);
            var currentImageHeight = parseInt(resizeId.height);

            resizeEndCoordsX = e.clientX;
            resizeEndCoordsY = e.clientY;

            resizeId.style.height = currentImageHeight - (resizeStartCoordsY - resizeEndCoordsY) + 'px';
            resizeId.style.width = currentImageWidth - (resizeStartCoordsX - resizeEndCoordsX) + 'px';

            resizing = false;
          }
          return false;
        }
      }
      </script>
    </head>

    <body>
      <img id="resizeImage" src="https://upload.wikimedia.org/wikipedia/commons/e/e7/Mozilla_Firefox_3.5_logo_256.png" 
width="64" height="64" />
      <p>Click on the image and drag for resizing.</p>
    </body>

  </html>

Example: Drawing Lines

FIXME: Need Content. Or, remove headline

ডকুমেন্ট ট্যাগ এবং অবদানকারী

 Contributors to this page: teoli, ershadul
 সর্বশেষ হালনাগাদ করেছেন: teoli,