Short Tag – Web Development Blog

Entries for the ‘Javascript’ Category

Preload images with plain javascript

This is a continuation of my previous post, preloading images with jQuery.
More info about preloaders: here.
This articles is for those who wish to use preloaders but do not use jQuery.

function preload(images)
{
for (var i = 0; i < images.length; i++)
{
[...]

Preload images with jQuery

I will present 2 ways of preloading images with jQuery.
The first one is pre-loading images from an array, the second one is preloading image paths stored in a special attribute.
Pre-loading images means we want to load the images when the page is loading, so we can have them loaded when we want to use them. [...]

jQuery inArray function

Now, thanks to jQuery we have a javascript alternative for PHP’s in_array function.
Let’s have an array:

var my_array = ["I", "love", "short-tag.info", 99];

It will work with mixed arrays as you can see in the example above.
Now, let’s do a few searches:

var pos1 = jQuery.inArray("love", my_array);

It will return 1, since this is the index of the 2nd [...]

jQuery treeview Expand all / Collapse all not working

In jQuery 1.3.2 there was a change in how :visible and :hidden selectors work.
Here is more info: http://docs.jquery.com/Release:jQuery_1.3.2#:visible.2F:hidden_Overhauled
This change affects directly the functionality of the treeview plugin of jQuery.
I am referring to this one: http://jquery.bassistance.de/treeview/demo/
The expand all functionality will be effected the most since it will be visible for the users that the tree is [...]

Fancybox – width and height not working

Let’s say you have this code:

$.fancybox({
‘autoScale’ : false,
‘transitionIn’ : ‘none’,
‘transitionOut’ : ‘none’,
‘width’ : ‘680′,
‘height’ : ‘495′
});

If you use Fancybox like this the width and height of the window will not be 680×495. Instead the window will have almost the size of the browser window. This is an often encountered problem.
The solution is to remove the quotes from the width and [...]

Make cPanel Api2 Requests with Jquery and Javascript

First, include jQuery in your page:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Here is the API 2 of cPanel:
http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api2/WebHome
Find the modules and function names you need, and use them when calling the function below.
Copy the following function to your webpage:

function cpanel_call(module, fct, domain, user, password, secure, port, json, callback, return_as, async)
{
var protocol = secure ? "https" : "http";
var data_type = json [...]

Xul and XPCOM: Delete files

Here is a little handy function for removing files with Javascript on Mozzila platform:

function delete_file_by_path(path)
{
var file = Components.classes['@mozilla.org/file/local;1']
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
if (file.exists())
file.remove(false);
}

Create a new instance of nsILocalFile, and the use the initWithPath function to initialize the file, and the delete the file is exists.

Javascript function to trim any character

Today I want to share a javascript function that will trim any character from the beginning and the end of the string.
The function will trim all occurrences of the character from the beginning and the end of the string.

function trim_chars(str, char)
{

while (str.substr(0, 1) == char)
str = str.substr(1);

var nrt = str.length;
while (str.substr(nrt-1, 1) == char)
{
str = [...]

jQuery animate() is not working on element’s children in IE8

Today I encountered a problem I thought I should post about.
When you try to animate an element that has children (for example DIVs) positioned relative or absolute on Internet Explorer 8 these children will not animate, in fact they will not do anything the parent does. I tested this animating the opacity.

<div class="main">
<div class="first"></div>
<div class="second"></div>
<div [...]

Frames (frameset) detection with javascript

Even if I don’t like using framesets sometimes I have to, so when the user comes from a search engine he/she should be able to see the hole frameset, not only one page.
So, I found some frameset detection techniques.
1.) self == top
If the value is `true` it means we have no frames, if the values [...]