Found 8591 Articles for Front End Technology

How to convert the image into a base64 string using JavaScript?

Arjun Thakur
Updated on 24-Jun-2020 06:36:26

604 Views

To convert the image into a base64 string using JavaScript, use the FileReader API. You can try to run the following code to get base64string for an image −Example                    function toDataURL(url, callback) {             var httpRequest = new XMLHttpRequest();             httpRequest.onload = function() {                var fileReader = new FileReader();                   fileReader.onloadend = function() {                      callback(fileReader.result);                   }                   fileReader.readAsDataURL(httpRequest.response);             };             httpRequest.open('GET', url);             httpRequest.responseType = 'blob';             httpRequest.send();          }          toDataURL('https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg', function(dataUrl) {          document.write('Result in string:', dataUrl)       })          

How to fix Array indexOf() in JavaScript for Internet Explorer browsers?

Ramu Prasad
Updated on 24-Jun-2020 06:37:02

458 Views

To fix Array.indexOf() for Internet Explorer web browser, use the following −jQuery.inArray( value, array [, fromIndex ] )Add the following,

How to iterate a JavaScript object's properties using jQuery?

Lokesh Badavath
Updated on 18-Nov-2022 12:00:20

2K+ Views

In this article, we are going to learn how to iterate a JavaScript object’s properties using jQuery. The simplest way to iterate over an object with JavaScript is to use a for in loop. The for statement will iterate over the objects as an array, but the loop will send the key to the object instead of an index as a parameter. This loop is used to iterate over all non-Symbol iterative properties of an object. The hasOwnProperty() method can be used to check if the property belongs to the object itself. The value of each key of the ... Read More

How to work with JavaScript Drag and drop for touch devices?

Lokesh Badavath
Updated on 18-Nov-2022 11:54:51

5K+ Views

In this article, we are going to discuss how to work with JavaScript Drag and drop for touch devices in JavaScript. Using JavaScript, we can only drag an image and some text. To drag an image, we simply hold the mouse button down and then move it. To drag the text, we need to highlight some text and drag it in the same way as image. HTML5 specifies almost all elements can be draggable. To make an element draggable, we add the draggable property with the value of true to its HTML element tag. Syntax Following is the syntax ... Read More

How to secretly copy to clipboard JavaScript function in Chrome and Firefox?

Lokesh Badavath
Updated on 18-Nov-2022 11:47:34

1K+ Views

In this article we are going to try how to secretly copy a JavaScript function to the clipboard. We use the copytext() method to secretly copy JavaScript function to the clipboard. These functions work on JavaScript console as well. To understand better let’s look into the examples one by one. Example Following is the example program we used copyText() method to copy text to clipboard using JavaScript function. Copy text ... Read More

Is it correct to use JavaScript Array.sort() method for shuffling?

Nikitha N
Updated on 24-Jun-2020 06:40:54

89 Views

Yes, you can use the JavaScript Array.sort() method for shuffling. Let’s see howExamplefunction shuffleDisplay(arr) {    var tmp, current;        // calculating length    var top = arr.length;    if(top) while(--top) {       current = Math.floor(Math.random() * (top + 1));       tmp = arr[current];       arr[current] = arr[top];       arr[top] = tmp;    }    return arr; }

Why is [1,2] + [3,4] = “1,23,4” in JavaScript?

Arjun Thakur
Updated on 24-Jun-2020 06:41:33

100 Views

The JavaScript's + operator is used to add two numbers or join two strings. However, use the contact() method to join two arrays to get a new one. For example,[50, 70].concat([90, 100])The above prints,[50, 70, 90, 100]Let’s see your example. The + operator concats strings, and converts the arrays to strings −[1,2] + [3,4] '1,2' + '3,4' 1,23,4Or as mentioned above, use concat(),[1,2].concat([3,4]) [1,2,3,4]

How to display JavaScript variables in an HTML page without document.write?

Sravani S
Updated on 24-Jun-2020 06:42:02

426 Views

Use Element.innerHTML in JavaScript to display JavaScript variables in an HTML page without document.write.You can try to work through the following code snippet −var $myName = document.querySelector('.name'); var $jsValue = document.querySelector('.jsValue'); $myName.addEventListener('input', function(event){    $jsValue.innerHTML = $myName.value; }, false);

How to create SVG graphics using JavaScript?

Govinda Sai
Updated on 24-Jan-2020 06:52:31

632 Views

All modern browsers support SVG and you can easily create it using JavaScript. Google Chrome and Firefox both support SVG.With JavaScript, create a blank SVG document object model (DOM). Using attributes, create a shape like a circle or a rectangle.var mySvg = "http://www.w3.org/2000/svg"; var myDoc = evt.target.ownerDocument; var myShape = svgDocument.createElementNS(mySvg, "circle"); myShape.setAttributeNS(null, "cx", 40); myShape.setAttributeNS(null, "cy", 40); myShape.setAttributeNS(null, "r", 30); myShape.setAttributeNS(null, "fill", "yellow");

How to convert a binary NodeJS Buffer to JavaScript ArrayBuffer?

Chandu yadav
Updated on 24-Jun-2020 06:28:30

797 Views

Access the buf.buffer property directly to convert a binary NodeJS Buffer to JavaScript ArrayBuffer. The write through the original Buffer instance writes the ArrayBufferView.Keep in mind that the instances of Buffer are also instances of Uint8Array in node.js 4.x and higher versions.ExampleYou can try the following code snippet to convert a NodeJS buffer to JavaScript ArrayBuffer −function toArrayBuffer(myBuf) {    var myBuffer = new ArrayBuffer(myBuf.length);    var res = new Uint8Array(myBuffer);    for (var i = 0; i < myBuf.length; ++i) {       res[i] = myBuf[i];    }    return myBuffer; }

Advertisements