Posts

Showing posts with the label javascript

How to round the number up to 2 digits in Javascript

Sometimes you need to round up the number up to 2 digits. For example, you calculation has to show a result in currency. There are two solutions to use on internet: 1) Use Math.random function. var a=123.4567; Math.random(a*100)/100 However, this is not really good, as for example in case of var a=1.005; Math.random(a*100)/100 results into 1 instead of 1.01. 2) use toFixed(2) However, it is more secure option to use a.toFixed(2), which would return element as fixed 2 decimal value

Javascript - Take care of Regular Expressions

These text here is my summary of things I learned while reading the eloquentJS book. In order to understand the issues mentioned in a book better, I have created a different examples. Take care when using Regular expressions in Javascript. 1) lastIndex is not refreshed, when using global regular expressions for multiple exec calls.

Java Expert RE-Learning Javascript Part 2 - Operators

What do you thing, what does javascript do with the following ? "5" + 1 - result? "51" 5 + "1" - result? "51" "5" - 1 - result? 4 "5" == 5 - result? true null == undefined - result? true How did you do? I was bad at it, especially to realize that "5" + 1 and 5 + "1" would make the same result. Javascript tries always to do conversion whenever the types of operands dont match. Javascript is pretty easy-going language. It allows you to do many things, giving you a bit of additional freedom. BUT you have to be VERY careful, as you can imagine that in some cases, above mentioned freedom might cost you a lot of bugs :) So, summary of rules in Javascript about operators: If you give Javascript different types to operate with, it will start doing type conversion. The way it would do it is based on set of (often confusing) rules. It would check first the operator, and then try to convert opera...

Java Expert RE-Learning Javascript Part 1 - Intro

You know the time when you come across so many different programming languages, it happens that you come cross programming language you never really learn from scratch, but just "learn by doing". It has been like that all the way until I had "awakening" call, a job interview, where I got javascript questions which I did not know at all. The biggest issue was its similarity to Java/C programming languages, where many things I rather "assumed" it is the same, without ever really checking it out. After sobering up, I decided to RE-learn Javascript, and started reading book "Eloquent Javascript"  . after reading now already a quarter of a book, I am positively surprised how nice it was written. I strongly suggest to anyone, either a core beginner in programming (no pre-knowledge whatsoever is required to read it), or even a veteran programmer. This book has also very challenging exercises in the end which solutions (can be found on above link)...

CodeAnywhere, (thumbsup)!

I started some mini javascript project of my own, so needed to create a relatively simplistic development environment. However, this environment has some constrains: - it should not require install (portable to many different devices), - it should connect to my Dropbox  account. For that purpose, I came across CodeAnywhere  website, where you can for free set up your account and connect your dropbox account and immediately start working. However, the internal browser that they have is running through their proxy, which causes a number of issues for my javascript development, so I am not using it.

Javascript highlighter, markup editor, WYSIWYG editor plugins

Depends on what you need, but the following plugins are available in terms of markup presenting/editing plugins I would divide into three main types, Syntax Highlighter plugins - it highlights a code so that it is easier to read Markup editor plugins - turns textareas into rich and powerful markup editors WYSIWYG editor plugins - high level editors for end-users Syntax Highlighter plugins  So far, I have seen several of them: SyntaxHighlighter plugin [demo] [github] or [demo] [github] Snippet [link] Google Prettify [link]   Hightlight.js [link]   SHJS [link]   Chilli JS [link]   beautyOfCode [link] Lighter.js [link] DlHighlight [link] JUSH [link] Markup editor plugins CodeMirror [link] along with CodeMirror UI [link]   MarkItUp! [link] SmartMarkUP [link]   WYSIWYG editor plugins  I personally really like those inline-smooth editors, so in that regard I have places a little star on those that cought my...

jQuery 2.0, planned for early 2013, will drop the support for IE6-7-8

Sky High Code: jQuery 2.0 ends WinXP support - Good. Interesting article, interesting news from jQuery! It is seriously great step for jQuery to stop the support of IE8,7,6, depicting overall programmers opinion about these versions. As what jQuery officials have explained on ( source ), all those hooks used to fix oldIE compatibility issues made the jQuery run slower for modern browsers. Minding the percentage of IE6-7-8 users, this is a logical step forward. Luckily, starting from early 2013, when this change is planned, would mean adding a simple condition checking whether browser is IE8- and loading jQuery1.9, and otherwise loading jQuery2.0. I could imagine a issues that people using CDN and latest jQuery versions would come up with, if they forget to make the correct condition switch. However, 1) If my clients aren't willing to stay current, then it's time for me to find new clients I dont think everyone can change their clients like that. Even suggesting to...

Google Developers Blog: Better Web Templating with AngularJS 1.0

I have tried out a bit AngularJS. I have to say I am sad to get to know it so late, because it could have been of grate use for certain html form related project. I really like the dynamic web templating of AngularJS, as you it makes the overall code very neat and easy to read. Source: Google Developers Blog: Better Web Templating with AngularJS 1.0 : By MiÅ¡ko Hevery , Google AngularJS team AngularJS lets you write web applications as if you had a smarter browser.  It lets you extend ...

A pew-pew manifesto...

Interesting discussion about Javascript by Bernd Paradies from Adobe. JavaScript is not suitable for large web apps

jQuery print example

This is just a small update of BenNadel 's great post on printing the part of the page using jQuery/Javascript. The code is placed here

Using AJAX call to send custom post data from the form

This was my solution and probably there are the better ones: jQuery.ajax({ url : //URL type: 'post', data: {'data' : JSON.stringify(data)}, dataType: 'html', cache: false, async : false, error: function (jqXHR, textStatus, errorThrown) { document.open(); document.write(res); document.close(); }, success: function (result) { document.open(); document.write(result); document.close(); } }); It is interesting to note that replacing whole html page via jQuery('html').html(result); does not work in IE9. Summary: if you wanna replace the whole page with newly html-formatted result, you have to use document.open();document.write(NewHTMLDocAsString);document.close(); instead of jQuery('html').html(result);

Javascript Prototype, sort of a object inheritance

Very cool thing to know about javascript: http://msdn.microsoft.com/en-us/scriptjunkie/ff852808

Javascript comparison operators

I came across very nice short explanation of the differences between == and === comparison operators. I have copied this into my blog just in case the post gets deleted from stackoverflow for whatever reason. The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal. Reference: Javascript Tutorial: Comparison Operators The == operator will compare for equality after doing any necessary type conversions . The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same. To quote Douglas Crockford's excellent JavaScript: The Good Parts , JavaScript has two sets of equality operators: === and !== , and their evil twins == and != . The good ones work the way you would expect. ...

When IE doesnt parse the received XML AJAX response...

Take care that configured dataType of your ajax call is set to 'xml' instead fo 'html' or 'json', since otherwise IE will interpret it as XML.

trim() not supported by IE7 and IE8

Funny fact, trim function is not supported by IE7 and IE8. Ok, it is supported, but through jQuery trim function. So, when you have something like this: jQuery(...).attr().trim() , this trim() function stops having any relation to jQuery, as the attr() would return String type, which method would be then trim(). Since IE7 and IE8 do not support trim() method within String type, you should use instead jQuery.trim(jQuery(...).attr()) or if you don't wanna use jQuery (whyyyy?), you can simply implement the trim function youself. if(typeof String.prototype.trim !== 'function') { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } } http://www.blogger.com/img/blank.gif source 1 source 2

Testing developments for different versions of Internet Explorer

I found excellent article on development environments different IE versions review, made By Addy Osmani Article can be found here I usually tend to take all the context from the page into this blog to make sure it exist also after the page disappears (you will be surprised how fast link gets changed, wither due to change of the link itself, or to site being closed down due to whatever reasons). This time I'll give it a risk. Just as a summary, for fast and simple testing, use F12 development mode, and for more detailed test, use the virtual machines and IECollection (you have to get yourself licensed versions of Windows (XP, Vista, and 7)), or use the Windows Virtual PC VHDs (read Notes, max 90 days of being able to permanently save any changes, made on the images). As a long time user of Virtual machines, I do like to use virtual machines, like VMware or VirtualBox , but still, the easiest way so far is to use F12 developer mode button on IE, and set the browser vers...

tabindex browser compatibility issue

When creating form, take care of the way how the fields are passed on when key is pressed. It is usually solved via tabindex, being set as an attribute to the form fields. However, if some of the elements in the following set {A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA} are not tabindexed, browsers will behave differently upon them. Internet Explorer will make them first in the order, and then those which are tabindexed, and Firefox would do opposite. Therefore you can either put to not tabindexed elements very large value (up to 32545), so they are shown always the last, or put tabindex="-1". I personally would not use the latter case, as it is not valid value for tabindex in its defition, so the browsers might change the way they behave upon it in the future. Some more details about it, I have found pretty nice article: http://www.codestore.net/store.nsf/unid/BLOG-20060706

Triggering event in a Form/Javascript

Sometimes the values you put in the form through any kind of way except user interaction are not triggering change event upon which some other parts of the form behave. In order to forcefully trigger the even, you can use jQuery. jQuery('YOUR_SELECTOR').trigger('change'); or simply $('YOUR_SELECTOR').trigger('change'); My Advice. ALWAYS use jQuery rather than any custom made functions, since jQuery is guaranteed to be web browser compatible! And it is cool and easy!