Category Archives: javascript

Determine original size of image cross browser?

Questions: Is there a reliable, framework independent way of determining the physical dimensions of a <img src=’xyz.jpg’> resized on the client side? Answers: You have 2 options: Option 1: Remove the width and height attributes and read offsetWidth and offsetHeight Option 2: Create a JavaScript Image object, set the src, and read the width and… Read More »

Keydown Simulation in Chrome fires normally but not the correct key

Questions: I want to simulate keydown events on a given textarea element in an html page. Since I am using chrome, I called initKeyboardEvent on my variable and I passed the keyCode I want to type into the textarea. Here is what I tried: var keyEvent = document.createEvent(‘KeyboardEvent’); keyEvent.initKeyboardEvent(‘keydown’, true, false, null, 0, false, 0,… Read More »

Display two decimal places, no rounding

Questions: Suppose I have a value of 15.7784514, I want to display it 15.77 with no rounding. var num = parseFloat(15.7784514); document.write(num.toFixed(1)+”<br />”); document.write(num.toFixed(2)+”<br />”); document.write(num.toFixed(3)+”<br />”); document.write(num.toFixed(10)); Results in – 15.8 15.78 15.778 15.7784514000 How do I display 15.77? Answers: Convert the number into a string, match the number up to the second decimal… Read More »

“innerHTML += …” vs “appendChild(txtNode)”

Questions: The question is, comparing concatination using innerHTML and appending a text node to an existing node. What is happening behind the scene? My thoughts around this so far: I’m guessing both are causing a ‘ReFlow’. The latter (appending a text node), from what I know, also causes a complete rebuild of the DOM (correct?… Read More »

Check if checkbox is checked JavaScript

Questions: I am building a mobile web app with jQuery Mobile and I want to check if a checkbox is checked. Here is my code. <script type=text/javascript> function validate(){ if (remember.checked == 1){ alert(“checked”) ; } else { alert(“You didn’t check it! Let me check it for you.”) } } </script> <input id=”remember” name=”remember” type=”checkbox”… Read More »

How to use Revealing module pattern in JavaScript

Questions: I stumbled across this post: JavaScript’s Revealing Module Pattern. I would like to use this in my project. Let’s imagine I have a function abc and I am calling that function in my main JavaScript file. Does this pattern make things different? Can anyone show me a basic example of this pattern? Answers: A… Read More »

How to go to a specific element on page? [duplicate]

Questions: This question already has an answer here: How can I scroll to a specific location on the page using jquery? 9 answers Answers: The standard technique in plugin form would look something like this: (function($) { $.fn.goTo = function() { $(‘html, body’).animate({ scrollTop: $(this).offset().top + ‘px’ }, ‘fast’); return this; // for chaining… }… Read More »