Sunday, February 3, 2013

Find size of file behind download link with jquery before download from server

You could make a HTTP HEAD request, and get a file size approximate by reading the Content-Length HTTP Header.
This kind of request is used to obtain meta-information about the URL implied by the request, without transferring any content of it in the response.
var xhr = $.ajax({
  type: "HEAD",
  url: "http://domain.com/file.ext",
  success: function(msg){
    alert(xhr.getResponseHeader('Content-Length') + ' bytes');
  }
});

Friday, February 1, 2013

Remove horizontal and vertical scrollbar from facebook apps

Set scroll=no and overflow:hidden to your body and use FB.Canvas.setAutoGrow() to remove the scrollbar.
<body scroll="no" style="overflow:hidden">
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function () {
    window.setTimeout(function () {
        FB.Canvas.setAutoGrow()
    }, 250)
};
(function () {
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e)
}());
</script>
 
Follow the link. 
Remove-horizontal-and-vertical-scrollbar-from-facebook-apps 

get file size by jquery before upload

You actually don't have access to filesystem (for example reading and writing local files), however, due to HTML5 File Api specification, there are some file properties that you do have access to, and the file size is one of them.
For the HTML bellow
<input type="file" id="myFile" />
try the following:
//binds to onchange event of your input field
$('#myFile').bind('change', function() {

  //this.files[0].size gets the size of your file.
  alert(this.files[0].size);

});
As it is a part of the HTML5 specification, it will only work for modern browsers (and IE is not one of them).

php array delete by value (not key)

Using array_seach(), try the following:
if(($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a "falsey" value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.
The if() statement will check whether array_search() returned a value, and will only perform an action if it did.

View print cakephp generated mysql query

$log = $this->controller->UserRequestFeed->getDataSource()->getLog(false, false);
debug($log);

Tuesday, January 22, 2013

A find condition about hasMany(has many) relationship in cakephp

Suppose you have two model User and UserRole.
And User has many UserRole.
Now you want to find User By specific UserRole.

User:
Fields: id, name, others........
var $hasMany = array(
    "UserRole"
);

UserRole:
Fields: id, user_id, role_id, others...var 
$belongsTo = array("User");

First you need to find all UserRole by your own conditions:

$userRoles = $this->UserRole->find("all", array(
    "conditions" => array(
        "UserRole.condition" => $value
    )
));

Now find User by UserRole as follows:

$users = $this->User->find("all", array(
    "conditions" => array(
        "User.id" => Set::extract("/UserRole/user_id", $userRoles)
    )
));


BINGO...

Monday, January 21, 2013

stop a page from exit or unload using jquery

<html>
<head>
<title>Refresh a page in jQuery</title>
 
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
 
</head>
 
<body>
 
<h1>Stop a page from exit with jQuery</h1>
 
<button id="reload">Refresh a Page in jQuery</button>
 
<script type="text/javascript">
 
 $('#reload').click(function() {
 
   location.reload();
 
 });
 
 $(window).bind('beforeunload', function(){
  return 'Are you sure want to leaving?';
 });
 
</script>
 
</body>
</html>