Short Tag – Web Development Blog

Php .Htaccess Set default timezone

When getting an error like this:

© Warning: date(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘America/Location’ for ‘EST/-5.0/no DST’

It means you have to set the default timezone of your server.
1.) You can do this by changing php.ini
[Date]
; Defines the default timezone used by the date functions
date.timezone = America/Location

2.) You can set it up in htaccess
SetEnv TZ America/Location

3.) You can change it in php
date_default_timezone_set(“America/Location”);

The entire list of  timezones can be found on the PHP site:
http://www.php.net/manual/en/timezones.php

Replace America/Location with your server’s timezone.

Loop through a Combobox, Listbox in XUL/XPCOM

So, we have a XUL component, that looks like this:

<listbox id="listboxId">
<listitem label="1" />
<listitem label="2" />
<listitem label="3" />
</listbox>

We can loop through the elements of this componenet using XPCOM:

var lbox = document.getElementById('listboxId');

for (var i =0; i<lbox.children.length; i++)
{
var child = lbox.children[i];
//do someting with child, like
alert(child.label)
}

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 = str.substr(0, nrt-1);
}

return str;
}

The function will be used like this:

trim_chars('/public_html/images/', '/');

In this case it will trim the ‘/’ from the beginning and the end of the string leaving: public_html/images

If you would like to trim the characters only from the beginning of the string, the function would look like this:

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

return str;
}

If you would like to trim only the last characters:

function trim_chars(str, char)
{
var nrt = str.length;
while (str.substr(nrt-1, 1) == char)
{
str = str.substr(0, nrt-1);
}

return str;
}

PHP str_replace

PHP’s str_replace function will replace every occurrence of the selected string with another string.

echo str_replace("car", "bike", "That car is red!");

The code above displays the text: That bike is red!
Function parameters:

str_replace(searched_string, replacement_string, target_string, count)

searched_string – the string that will be replaced
replacement_string – the string that will be used to replace the searched string
target_string – the string in which the replacement will occur
count – the number or replacements that will be performed, if it is not specified all of the occurrences will be replaced

Notes:

  • The count parameter has been added in PHP5
  • The function is binary safe
  • The function is case-sensitive (use str_ireplace() function for case insensitive replace)
  • The function replaces from left to right

You can use arrays in replacements too:

Case 1: replace all values from an array with a single value

$arr = array('orange', 'lemon', 'pear');
str_replace($arr, 'apple', 'orange is big, lemon is small, pear is good');

It will return: apple is big, apple is small, apple is good

Case 2: replace all values from an array with another values from another array

$arr = array('orange', 'lemon', 'pear');
$arr2 = array('Hulk', 'Alvin', 'Pizza');
str_replace($arr, $arr2, 'orange is big, lemon is small, pear is good');

It will return: Hulk is big, Alvin is small, Pizza is good

LaunchTAE14

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 class="third"></div>
</div>

If classes ’second’ and ‘third’ are positioned relatively or absolutely that will not be animated using this:

$(".main").animate({opacity : 0}, 500);

The best solution I found was to hide the elements after the animation has ended. So, after the animation ended I forced those DIVs to take their end values directly, no animation involved. This method will skip the animation only on IE8 and other browsers with the same problem.

First I needed to add another class to the problematic DIVs:

<div class="main">
<div class="first"></div>
<div class="second problem"></div>
<div class="third problem"></div>
</div>

And I modified the Javascript like this:

$(".main").animate({opacity : 0}, 500, function(){$(".main").find(".problem").css('opacity', 0);});

Xul Javascript: Difference between getService() and createInstance()

When calling an XPCom component from Javascript we can use to functions: getService() and createInstance().
* getService() will get a singleton class, so a single object will do all the work no matter how many times you call it.
* createInstance() will create a new instance of that class, this is useful in situations where you make asynchronous requests, or process things in parallel and you don’t want all of it to be processed by the same object.

Example:

var file = Components.classes["@mozilla.org/file/directory_service;1"]
.createInstance(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);

Get and Set Preferences in XUL

Below are two funtions that will set and get boolean values in your firefox profile. These can be used in standalone XULRunner applications or in Firefox extensions.

Use them to save the settings of your application / extensions.

function setSettingValue(setting, value)
{
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService).getBranch("accessibility.");

prefs.setBoolPref(setting, value);
}

function getSettingValue(setting)
{
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService).getBranch("accessibility.");

return prefs.getBoolPref(setting);
}

For setting/getting int type preference use: getIntPref() and setIntPref().
For string preferences use: getCharPref() and setCharPref().

More details on MDN:
https://developer.mozilla.org/en/Code_snippets/Preferences

Cross-browser CSS code to disable selectable text

Maybe you created your own custom button from divs, or you just want to disable the ability to select text on an element (Usually is used for elements that act like buttons).

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;

Use all these selectors on the element. This should work on all browsers.

Remove all children from an element using XUL Javascript

This is a small script that will remove all children of a XULElement:

var elem = document.getElementById('my_element');
while (elem.firstChild)
elem.removeChild(elem.firstChild);

Open an URL with XUL and XPCom

Since the HTML <a> tag is not working in XUL we need another approach to open a URL.
You can use the function below in your project:

function gotoUrl(url)
{
var nsioservice= Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);

var uri = nsioservice.newURI(url, null, null);

var eps = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Components.interfaces.nsIExternalProtocolService);

eps.loadURI(uri, null);
}

Use it like this:

gotoUrl('http://www.short-tag.info');