PPTwo Lines Client-Side JavaScript to Server-Side PHP

In : PHP, Posted by admin on Mar.03, 2009

It is a very useful helper function for PHP and Javascript.

http://phpolait.sourceforge.net/doc/index.html

PHP-O-Lait version 0.5.1 is released, with a bug fix that caused the jsolait.js JavaScript library not to be included correctly in certain circumstances. Many thanks to Olaf Bottek for locating the bug and assisting in correcting it. A few documentation changes have also been made, including an example of using PHP-O-Lait for asynchronous communication: thanks to Keith Powell for the idea.

Tags :


Javascript for dynamic portal similar to netvibes

In : Uncategorized, Posted by admin on Mar.03, 2009

DHTML goodies has a downloadable javascript to build dynamic portal similar to http://www.netvibes.com and http://www.pageflakes.com.

The code is not very well organize. However, it is a start if you want to build a dynamic portal.

http://www.dhtmlgoodies.com/scripts/dragable-boxes/dragable-boxes.html

Tags :


How can I insert a character entity like &nbsp into a document from Javascript ?

In : Uncategorized, Posted by admin on Mar.03, 2009

If you want to insert a non-breaking space you can insert it using the
charactor code. Like this:

var nbsp = document.createTextNode( "\u00A0" );
referenceToWhereYouWantIt.appendChild( nbsp );

The url above is to the complete ECMA Spec.

Tags :


Dynamic Javascript Execution

In : Uncategorized, Posted by admin on Mar.03, 2009

A lot of time when you do an AJAX call, you want to dynamic execute the javascript within the AJAX updated region. For example:

<div id="ajax_update_here">

<!-- this is ajax return result -->
<script>
alert("this is a dynamic message from the ajax backend");
</script>
<!-- end of ajax return result -->

</div>

http://kratcode.wordpress.com/tag/javascript/ is a description of how to do it. Just call: execJS(‘ajax_update_here’); and it will execute the javascript within the div block.

function execJS(node)
{
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

  if (!node) return;

  /* IE wants it uppercase */
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;

  for(var i=0;i<st.length; i++)
  {
    if (bSaf) {
      strExec = st[i].innerHTML;
      st[i].innerHTML = "";
    } else if (bOpera) {
      strExec = st[i].text;
      st[i].text = "";
    } else if (bMoz) {
      strExec = st[i].textContent;
      st[i].textContent = "";
    } else {
      strExec = st[i].text;
      st[i].text = "";
    }

    try {
      var x = document.createElement("script");
      x.type = "text/javascript";

      /* In IE we must use .text! */
      if ((bSaf) || (bOpera) || (bMoz))
        x.innerHTML = strExec;
      else x.text = strExec;

      document.getElementsByTagName("head")[0].appendChild(x);
    } catch(e) {
      alert(e);
    }
  }
};
Tags :


Restart rails deploy in Dreamhost

In : ruby, Posted by admin on Mar.03, 2009

If you are using Dreamhost to deploy Ruby On Rails, I suggest you to look into:

Webistrano – http://labs.peritor.com/webistrano

This is a web UI to make PHP, rails deploy a lot easier. 5 stars!

Also, in case you change the source code in Dreamhost and want Dreamhost to reload the file, you see to do the following:

  1. SSH to your Dreamhost account
  2. echo “” > YOUR_DEPLOY_DIR_NAME/tmp/restart.txt
  3. This is trigger dreamhost’s Passenger to restart your files. Also, at the end, it will delete the tmp/restart.txt file.


How to do javascript subclassing

In : Uncategorized, linux, mysql, Posted by admin on Mar.03, 2009

http://www.golimojo.com/etc/js-subclass.html


function subclass(constructor, superConstructor)
{
function surrogateConstructor()
{
}

surrogateConstructor.prototype = superConstructor.prototype;

var prototypeObject = new surrogateConstructor();
prototypeObject.constructor = constructor;

constructor.prototype = prototypeObject;
}

Tags :


How to sort data in a big file

In : Uncategorized, Posted by admin on Mar.03, 2009

This is from wikipedia (http://en.wikipedia.org/wiki/External_sorting)

One example of external sorting is the external mergesort algorithm. For example, for sorting 900 megabytes of data using only 100 megabytes of RAM:

1. Read 100 MB of the data in main memory and sort by some conventional method (usually quicksort).
2. Write the sorted data to disk.
3. Repeat steps 1 and 2 until all of the data is sorted in 100 MB chunks, which now need to be merged into one single output file.
4. Read the first 10 MB of each sorted chunk (call them input buffers) in main memory (90 MB total) and allocate the remaining 10 MB for output buffer.
5. Perform a 9-way merging and store the result in the output buffer. If the output buffer is full, write it to the final sorted file. If any of the 9 input buffers gets empty, fill it with the next 10 MB of its associated 100 MB sorted chunk or otherwise mark it as exhausted if there is no more data in the sorted chunk and do not use it for merging.

This algorithm can be generalized by assuming that the amount of data to be sorted exceeds the available memory by a factor of K. Then, K chunks of data need to be sorted and a K-way merge has to be completed. If X is the amount of main memory available, there will be K input buffers and 1 output buffer of size X/(K+1) each. Depending on various factors (how fast the hard drive is, what is the value of K) better performance can be achieved if the output buffer is made larger (for example twice as large as one input buffer).

In the example, a single-pass merge was used. If the ratio of data to available main memory is particularly large, a multi-pass sorting is preferable. For example, merge only the first half of the sorted chunks, then the other half and now the problem has been reduced to merging just two sorted chunks. The exact number of passes depends on the above mentioned ratio, as well as the physical characteristics of the hard drive (transfer rate and seeking time). As a rule of thumb, it is inadvisable to perform a more-than-20-to-30-way merge.



Another silly interview question about how to swap 2 variables without temporary variable

In : Uncategorized, Posted by admin on Mar.03, 2009

How to swap 2 variables without using a temporary variable. Silly…

a=a+b;
b=a-b;
a=a-b;



Find MYSQL Duplicate records

In : mysql, Posted by admin on Mar.03, 2009

If the database has a table call employees. The values are:


ID           Name
1              a
2              b
3              a
4              c
5              b

You want to find the duplicate record but not the first one (ie, ID# 3 and 5). You can do the following:

select id from employees where id not in (select distinct id from employees group by name);



CSS box model

In : Uncategorized, Posted by admin on Mar.03, 2009

Each box has a content area (e.g., text, an image, etc.) and optional surrounding padding, border, and margin areas; the size of each area is specified by properties defined below. The following diagram shows how these areas relate and the terminology used to refer to pieces of margin, border, and padding:

The margin, border, and padding can be broken down into left, right, top, and bottom segments (e.g., in the diagram, “LM” for left margin, “RP” for right padding, “TB” for top border, etc.).

The perimeter of each of the four areas (content, padding, border, and margin) is called an “edge”, so each box has four edges:

content edge or inner edge
The content edge surrounds the element’s rendered content.
padding edge
The padding edge surrounds the box padding. If the padding has 0 width, the padding edge is the same as the content edge. The padding edge of a box defines the edges of the containing block established by the box.
border edge
The border edge surrounds the box’s border. If the border has 0 width, the border edge is the same as the padding edge.
margin edge or outer edge
The margin edge surrounds the box margin. If the margin has 0 width, the margin edge is the same as the border edge.

Each edge may be broken down into a left, right, top, and bottom edge.

The dimensions of the content area of a box — the content width and content height — depend on several factors: whether the element generating the box has the ‘width’ or ‘height’ property set, whether the box contains text or other boxes, whether the box is a table, etc. Box widths and heights are discussed in the chapter on visual formatting model details.

The box width is given by the sum of the left and right margins, border, and padding, and the content width. The height is given by the sum of the top and bottom margins, border, and padding, and the content height.

The background style of the various areas of a box are determined as follows:

  • Content area: The ‘background’ property of the generating element.
  • Padding area: The ‘background’ property of the generating element.
  • Border area: The border properties of the generating element.
  • Margin area: Margins are always transparent.

Problem with IE css hack

According to the W3C, an assigned ‘width’ (and ‘height’) of a box refers to the ‘content area’ of a box only. The padding, borders, and margins are then added to this value to arrive at the total box width. If the ‘width’ property is omitted, the total box width is the same as the ‘content area’ of the surrounding container element.

All well and good. Unfortunately, all CSS enabled versions of IE before IE6/strict use a different box model. In that model, the padding and borders are counted as part of any assigned ‘width’ or ‘height’. In the absence of borders and padding, the two models agree. However, if a box has an assigned “width’, and if borders and/or padding are added, the standard box model causes the overall box width (between the outer border edges) to increase, while in IE’s model the ‘content area’ gets squeezed by the same amount. This is a major problem for proper page layout.

Consider the following CSS:

{width:100px; padding:10px; border:10px;}

When viewed in a ’standards’ browser the dimension from border edge to border edge will be ‘140px’. (100+10+10+10+10=140) Because IE5.x puts all these values inside the ‘width’, the border edge to border edge dimension will be ‘100px’.

Note: For technical reasons it sometimes would be desirable to employ the old IE box model. It has been bruited about that CSS-3 will feature a way to choose between the two models, but the current standard model will no doubt remain the default.

Source: http://css-discuss.incutio.com/?page=BoxModelHack

Tags :