Change image source with JavaScript
function changeImage(a) so there is no such thing as a.src => just use a. demo here
function changeImage(a) so there is no such thing as a.src => just use a. demo here
The RegExp constructor takes a string and creates a regular expression out of it. function name(str,replaceWhat,replaceTo){ var re = new RegExp(replaceWhat, ‘g’); return str.replace(re,replaceTo); } If replaceWhat might contain characters that are special in regular expressions, you can do: function name(str,replaceWhat,replaceTo){ replaceWhat = replaceWhat.replace(/[-\/\\^$*+?.()|[\]{}]/g, ‘\\$&’); var re = new RegExp(replaceWhat, ‘g’); return str.replace(re,replaceTo); } See … Read more
Here you can research how to create an extension and download the sample manifest.json. Content Scripts can be used to run js/css matching certain urls. manifest.json { “name”: “Append Test Text”, “description”: “Add test123 to body”, “version”: “1.0”, “permissions”: [ “activeTab” ], “content_scripts”: [ { “matches”: [“http://*/*”], “js”: [“content-script.js”] } ], “browser_action”: { “default_title”: “Append … Read more
Since you put a asp.net-mvc tag on your post, I’m giving you IIS configuration solution for you. Chrome doesn’t consume woff and throws 404 error when your web server isn’t configured with MIME type ‘woff’ or ‘woff2’. You need to add IIS a MIME-TYPE for woff2. You can configure it in web.xml. <system.webServer> <staticContent> <remove … Read more
First, JavaScript and TypeScript behave the exact same way even if you write like that: theElement.addEventListener(“click”, onClick); Second, this is how you can retain a reference to an anonymous function: var f = (event) => this.onClick(event); theElement.addEventListener(“click”, f); // later theElement.removeEventListener(“click”, f); If you’re dealing with event listeners, there’s a useful pattern for your class … Read more
This article (#3) ran some performance tests and found that the jQuery .each function was about 10x as slow as the native javascript for loop. From 10 Ways to Instantly Increase Your jQuery Performance – 3. Use For Instead of Each Using Firebug, it’s possible to measure the time each of the two functions takes … Read more
You can do this using a hidden iFrame (I’m using jquery for the example): function loadOtherPage() { $(“<iframe>”) // create a new iframe element .hide() // make it invisible .attr(“src”, “/url/to/page/to/print”) // point the iframe to the page you want to print .appendTo(“body”); // add iframe to the DOM to cause it to load the … Read more
Edited (see comments) function cropImageFromCanvas(ctx) { var canvas = ctx.canvas, w = canvas.width, h = canvas.height, pix = {x:[], y:[]}, imageData = ctx.getImageData(0,0,canvas.width,canvas.height), x, y, index; for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { index = (y * w + x) * 4; if (imageData.data[index+3] … Read more
Assuming you’re not just seeing patterns where there aren’t any, try a Mersenee Twister (Wikipedia article here). There are various implementations like this one on github. Similar SO question: Seedable JavaScript random number generator If you want something closer to truly random, then consider using the random.org API to get truly random numbers, although I … Read more
You are already close: var getMonth = function(idx) { var objDate = new Date(); objDate.setDate(1); objDate.setMonth(idx-1); var locale = “en-us”, month = objDate.toLocaleString(locale, { month: “long” }); return month; } console.log(getMonth(1)); console.log(getMonth(12));