Category Archives: javascript

Download data url file

Questions: I’m playing with the idea of making a completely javascript based zip/unzip utility that anyone can access from a browser. They can just drag their zip directly into the browser and it’ll let them download all the files within. They can also create new zip files by dragging individual files in. I know it’d… Read More »

Can JavaScript connect with MySQL?

Questions: Can JavaScript connect with MySQL? If so, how? Answers: No, JavaScript can not directly connect to MySQL. But you can mix JS with PHP to do so. JavaScript is a client-side language and your MySQL database is going to be running on a server Questions: Answers: Client-side JavaScript cannot access MySQL without some kind… Read More »

JavaScript function declaration and evaluation order

Questions: Why does the first one of these examples not work, but all the other ones do? // 1 – does not work (function() { setTimeout(someFunction1, 10); var someFunction1 = function() { alert(‘here1’); }; })(); // 2 (function() { setTimeout(someFunction2, 10); function someFunction2() { alert(‘here2’); } })(); // 3 (function() { setTimeout(function() { someFunction3(); },… Read More »

A CORS POST request works from plain javascript, but why not with jQuery?

Questions: I’m trying to make a Cross Origin post request, and I got it working in plain Javascript like this: var request = new XMLHttpRequest(); var params = “action=something”;‘POST’, url, true); request.onreadystatechange = function() {if (request.readyState==4) alert(“It worked!”);}; request.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”); request.setRequestHeader(“Content-length”, params.length); request.setRequestHeader(“Connection”, “close”); request.send(params); But I would like to use jQuery, but I… Read More »

Declaring javascript object method in constructor function vs. in prototype [duplicate]

Questions: This question already has an answer here: Use of 'prototype' vs. 'this' in JavaScript? 13 answers Answers: For the example you give, you should use the prototype approach. In general, it depends. The main advantage of the first approach (initializing methods in the constructor) is that you can take advantage of closures by making… Read More »

Return multiple values in JavaScript?

Questions: I am trying to return two values in JavaScript. Is that possible? var newCodes = function() { var dCodes =; var dCodes2 =; return dCodes, dCodes2; }; Answers: No, but you could return an array containing your values: var newCodes = function() { var dCodes =; var dCodes2 =; return… Read More »

Trying to Validate URL Using JavaScript

Questions: I want to validate a URL and display message. Below is my code: $(“#pageUrl”).keydown(function(){ $(“.status”).show(); var url = $(“#pageUrl”).val(); if(isValidURL(url)){ $.ajax({ type: “POST”, url: “demo.php”, data: “pageUrl=”+ url, success: function(msg){ if(msg == 1 ){ $(“.status”).html(‘<img src=”images/success.gif”/><span><strong>SiteID:</strong>12345678901234456</span>’); }else{ $(“.status”).html(‘<img src=”images/failure.gif”/>’); } } }); }else{ $(“.status”).html(‘<img src=”images/failure.gif”/>’); } }); function isValidURL(url){ var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; if(RegExp.test(url)){… Read More »