Posts Under javascript Category
jQuery Colorbox not showing but background shows
I was using Colorbox for some lightbox type effects to show larger images when clicking on thumbnails, but for some reason the background was showing up but the pretty little box in the middle where the larger image is supposed to show never showed up, so this is how I made it finally appear after many failed attempts! $("document").ready(function(){ $(".theCssClass a[rel='forGroupingRelCodeGoesHere']").colorbox({ onLoad: function () { $("#colorbox").show(); } }); });…
Get Value of Checkbox using jQuery
This one is quite an easy one, but a good one to mention non-the-less as it does definitely come in really handy. If you would like to get a checkbox’s value to send to the backend using jQuery you can always do the following: Firstly let’s draw some HTML to illustrate our example. <input id="chkOurCheckbox" type="checkbox" /> Now we need some jQuery to do the real work. var ourCheckboxValue =…
Ternary Operation
If you do not know what the Ternary operator is, or do not use it while you are coding, let me be the first to tell you how much you are missing out! The Ternary operator looks like this: “?“, that’s right, it’s a question mark! My favourite way of using it is to shorten Conditional If Statements, so let me show you what I’m on about. The long way:…
IE6 PngFix in Javascript
As a web developer you will know just how much we all love hate Internet Explorer 6 – one of the most dreaded browsers and browser versions that still exists in the deep dark corners of the interwebs – and you will probably have figured out that it doesn’t play well with PNG images. So here is how you can resolve this problem. It’s not as nice as an actual…
How many days in February?
It is quite handy to have a function that can tell you how many days there are in February due to the leap year shift of either 28 or 29 days. Below is a function created in Javascript to return the exact amount of days according to the year given: function daysInFebruary (year){ return (((year%4==0)&&((!(year%100==0))||(year%400==0)))?29:28); }
Search and Replace Anchor Href using Javascript
I have a site that is under development and it was made really badly so I needed a quick way to replace all anchors on page to the relevant path. The problem is because it was a testsite that lived away from the public facing domain, the anchors had to be changed because they were all coded in as absolute paths. I included a JavascriptDebug file and put the following…
Swap HTML Elements Using Javascript
It is quite easy to use javascript to swap between divs or any other html dom elements. Using the function below you can pass the id of the element to show and to hide. function swapDivs(show, hide){ document.getElementById(show).style.display = "block"; document.getElementById(hide).style.display = "none"; }
[Exception... "Could not convert JavaScript" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"
What the heck is.. uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMHTMLDivElement.appendChild]” nsresult: “0×80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)” location: “JS frame :: PATH_TO_MY_JAVASCRIPT_FILE :: THE_METHOD_NAME :: line #” data: no] For me it was just an appendChild() that was causing the problems. You can always try the innerHTML+= way if all else fails
Stop mouse click in browsers
There have been multiple ways to stop your user/s from right clicking on your site, but many of them now fail due to the way firefox’s contextual menu loads. Below is an example of how to do it: function _c(e) {if (document.all) {if (event.button==2||event.button==3) {return false;}} else {if (e.button==2||e.button==3) {e.preventDefault();e.stopPropagation();return false;}}if (e.which) {}}if (document.all){document.onmousedown=_c;}else{document.onclick=_c;document.ondblclick=_c;}</span> ..and here is the full working example in a webpage: <html> <head> <script> function _c(e) {if…
Modify pages in a browser window
goto a url of your choice. enter the following into your address bar: javascript:document.body.contentEditable=”true”; document.designMode=”on”; void 0 Now you can edit the page.
Sending data from javascript to php
If you ever find yourself needing to do some ajax and in turn sending strings of characters via javascript then you should really try encodeURIComponent() to wrap your strings in. The function escape() can also be used, but I would not recommend it as encodeURIComponent() is better, it also escapes your usual ampersand ajax limiters which solved me quite a bit of pain.