Dynamically Resize an IFrame According to its contents
Tuesday, November 25, 2008 4:30 AM

problem with iframe is that you can't always predict the size of the content and eventually you'd end up with a scrollbar or even less convenient: a hidden part of the Iframe content

this is a straight forward, easy way to make sure that the IFrame is properly resized according to the contents of the page loaded inside the Iframe.

 

this will rely on the browser itself measuring the content width and height

this can be done by using the document.body.scrollHeight and document.body.scrollWidth properties which work the same way in all browsers IE and firefox

 

just to make sure that these properties work properly we'll set the initial width and height of the Iframe to 1 pixel so we can have a scroll bar to measure. if it's larger than that the scroll bars won't measure the whole width and height of the document and if it's 0 the reported scroll Area will be also Zero

 

body

one more issue to target is the margins. because we're using a subsection of the document which has the scroll Area Body does not include margins in that case we need to neutralize the effect of margins on the page. this can be done easily by setting the margins of the page to zero

 

resulting code would be in the Container page :

<iframe id="frameName" 
src="frame.html" width="1" height="1" 
frameborder="0" marginheight="0" marginwidth="0" 
scrolling="no"></iframe>

 

note that i have added the scrolling="no" attribute to make sure that there's no scrollbars displayed and i have also neutralized the border size by frameborder="0"

 

inner page would be like this in the End of the <body> i have added this simple javascrip

 

function doFrame(){            
    parent.document.getElementById("frameName").width= document.body.scrollWidth;
    parent.document.getElementById("frameName").height= document.body.scrollHeight;
}
doFrame();

 

note how i reference the frame by its Id.

by amir.magdy | 1 comment(s)
Filed under: ,
Google Browser Chrome
Monday, September 01, 2008 2:12 PM

Google have disclosed that they're going to release a new browser called Chrome. this is big news

here is the link,these are comics that google released to explain about the browser.

by amir.magdy | with no comments
Filed under:
Most Used Javascript Date functions
Thursday, August 21, 2008 12:16 PM

 

var OneDay = 1000*60*60*24;
 
function subtractDates (firstDate,secondDate){                    
    return Math.ceil((new Date(firstDate).getTime()
        -new Date(secondDate).getTime())/(OneDay));
}
 
 
function addDays (dateToAdd,amountInDays){
    return  new Date(dateToAdd.valueOf() + this.OneDay * amountInDays);
}
 
function daysInMonth (year,month){                       
    var m = [31,28,31,30,31,30,31,31,30,31,30,31];
    if (month != 2)
        return m[month - 1];
    if (year%4 != 0)
        return m[1];
    if (year%100 == 0 && year%400 != 0)
        return m[1]
    return m[1] + 1;
}
 
 
 
function isDate(dateStr){
    var ok;
    try{
    var d = Date.parse(dateStr);
        return true;
    }catch(x){
        return false;
    }
}
by amir.magdy | 1 comment(s)
Filed under:
Android 0.9
Monday, August 18, 2008 11:17 PM

Android 0.9 has just launched it has major UI updates

Download it here http://code.google.com/android/download_list.html

by amir.magdy | with no comments
Filed under:
Windows Fake URL test [hosts file]
Sunday, March 09, 2008 5:33 AM

you can make your application respond with a different behavior based on requested URL, This can be as simple as showing a different logo or as sophisticated as filtering data by that requested domain

this is not such a big implementation problem, but it seems hard to test that is if you don't know how to fake that URL request on your localhost

well

there's a file in this path on a windows environment

%windir%\system32\drivers\etc\hosts

this file contains the definition of the keyword "localhost"

   1: # Copyright (c) 1993-1999 Microsoft Corp.
   2: #
   3: # This is a sample HOSTS file used by Microsoft TCP/IP  for Windows.
   4: #
   5: # This file contains the mappings of IP addresses to  host names. Each
   6: # entry should be kept on an individual line. The IP  address should
   7: # be placed in the first column followed by the  corresponding host name.
   8: # The IP address and the host name should be separated  by at least one
   9: # space.
  10: #
  11: # Additionally, comments (such as these) may be  inserted on individual
  12: # lines or following the machine name denoted by a '#'  symbol.
  13: #
  14: # For example:
  15: #
  16: #      102.54.94.97     rhino.acme.com          #  source server
  17: #       38.25.63.10     x.acme.com              # x  client host
  18:  
  19: 127.0.0.1       localhost

 

this is the original contents of the file note in line 19 there's a record that states that 127.0.0.1 (which is the loopback address,, always pointing to your machine) has the alias localhost

 

so all you need to do to test a URL is add a record that states that the same IP is also your domain,

and also for subdomains you can still do the same, i'd recommend adding the subdomains before the domain itself

like this

 

   1: 127.0.0.1    mail.domain.com
   2: 127.0.0.1    user.domain.com
   3: 127.0.0.1    www.domain.com
   4: 127.0.0.1    domain.com

 

now when you browse to http://domain.com or http://user.domain.com it'll issue a request to your local Web server and using code you can redirect or display conditional content on the default website

ah.. don't forget to restart your browser after editing and also you need to be an administrator to edit this file

by amir.magdy | 2 comment(s)
Filed under:
2008 Launch Event in Egypt [heroes happen here]
Thursday, March 06, 2008 3:31 AM

on the 24th of march in Egypt

hero_event

 

register here

Internet Explorer Beta1 available for Download Now
Thursday, March 06, 2008 3:28 AM

ie8betalogo

 

here is the link  download it if u need to do testing on ur web applications, be ready.

by amir.magdy | with no comments
Filed under:
Measurement of Code Quality
Saturday, March 01, 2008 2:11 AM
there's a lot of ways to measure size of code, and Complexity of code, but up till this post there's nothing to measure the qualit of the code

it was always subjective to meaure the quality of code but after this.






sorry i had to post it, immediately after stumbling on it just now :D
by amir.magdy | 3 comment(s)
Filed under:
Yield in c#
Monday, February 25, 2008 3:47 AM

i've been trying to explain the keyword yield to a friend of mine for more than an hour and he did not get it.

but finally when i typed in this example he finally did

   1: class enu :IEnumerable
   2:   {
   3:       public IEnumerator GetEnumerator()
   4:       {
   5:           yield return 1;
   6:           yield return 2;
   7:           yield return 3;
   8:       }
   9:   }

 

it actually means that when you are enumerating this object it will return a different value/object on every iteration

so the next time i revised this sample it was way easier to grasp the multiple returns

   1: public class enu :IEnumerable
   2:  {
   3:      int _index=0;
   4:      int[] numbers = { 1, 2, 3, 4, 5 };
   5:      public IEnumerator GetEnumerator()
   6:      {
   7:          yield return numbers[_index];
   8:          _index++;
   9:      }
  10:  }

 

now that i've gone this far i have to say that the enumerator here returns are not of a specific type nothing in the previous code specifies that the return type is an int

so..

after C# 2.0 has introduced generics this would work fine

 

   1: public class enu :IEnumerable<int>
   2:  {
   3:      int _index=0;
   4:      int[] numbers = { 1, 2, 3, 4, 5 };
   5:      public IEnumerator GetEnumerator()
   6:      {
   7:          yield return numbers[_index];
   8:          _index++;
   9:      }
  10:  
  11:      IEnumerator<int> IEnumerable<int>.GetEnumerator()
  12:      {
  13:          yield return numbers[_index];
  14:          _index++;
  15:      }
  16:  }

 

note that this new Interface IEnumerable<T> needs both methods to be implemented

by amir.magdy | 10 comment(s)
Filed under: ,
Delegates to LINQ [passing logic as a parameter] (part I)
Friday, February 08, 2008 9:03 AM

it's normal to pass data to a function just thow in a parameter of the type of data you want to pass and ur set

public int add (int i,int u ){
    return i+u;
}

 

now think in a different way, u now want to build a set of operations that the user can choose from now what you need is to build a function that would take the user's input and the operation as parameters and put them together...

for instance he has a set of .. customers if you will and he wants to have them filtered in predefined filters like valuable customers , local customers and new customer.

Data that we're using looks like this

public class Customer {
    public int Id{ get; set;};
    public string Name{ get; set;};
    public DateTime FirstPurchaseDate{ get; set;};
    public string city{get; set;};
    public decimal TotalPurchase {get; set;}
}

we'll be passing that customer in a list of type customer

var customerList= new List<Customer>();

now for the logic, how to implement the three requested filter in a way they can be sent as a parameter to the search Method, well this is how it's done:

1-to do that we think of a way that we can reference a logic (a method) which is a delegate. So we'll build a delegate that can receive a Customer as a parameter and return weather it's valid customer according to the required criteria so the delegate's declaration should look something like this

public delegate bool evaluatetorDelegate(Customer customer);

 

2- we need to build a function that will actually do the evaluation but to do that it will have to have the same signature as the delegate; here we'll implement the three simple required functions

private const string LOCAL_CITY ="Alexandria"; //my city in Egypt :D
private const decimal VALUABLE_CUSTOMER_LIMIT = 10000.00;
 
public bool IsValuableCustomer(Customer customer){
    return customer.TotalPurchase > VALUABLE_CUSTOMER_LIMIT;
}
 
public bool IsLocalCustomer(Customer customer){
    return customer.City == LOCAL_CITY ;
}
 
public bool IsNewCustomer(Customer customer){
    return customer.FirstPurchaseDate > DateTime.ToDay.AddMonth(-1);
}

 

3- now we need to implement the function that will actually apply the filtering; should also  look something like this

public List<Customer> executeFilter(List<Customer> customers,
                                         evaluatetorDelegate filter) {
    List<Customer> resultList = List<Customer>();
    foreach (Customer customer in customers)
    {
        if (filter.Invoke(customer))
            resultList.Add(customer);
    }
    return resultList;
}

 

4- now that the example is complete let's see how can we utilize this code

public static void main(string param){}{
switch (param){
    case 1:
    evaluatetorDelegate d = IsLocalCustomer;
    break;
    case 2:
    evaluatetorDelegate d = IsNewCustomer;
    break;
    case 3:
    evaluatetorDelegate d = IsValuableCustomer;
    break;
}
List<Customers> Customers= GetCustomersFromDB ()// :D
// and then we call the method 
//then we apply the filtering we need
 
Customers = executeFilter (Customers,d);
 
// now the Customers contain only the customers which meet the criteria.
by amir.magdy | 3 comment(s)
Filed under: ,
DotnetKicks on CommunityServer.org [in 24 seconds]
Monday, December 31, 2007 1:10 AM

i didn't do this the right way i didn't go through documentation or consult with forums, i just wanted to do this the quick and easy way, what i do is i keep a sepparate theme for my blog sepparate from other bloggers so i can change whatever i like without hurting anyone's blogs

so what i really did is i edited to theme pages to show the dotnetkicks button

first the post.aspx page

<csblog:weblogpostdata Property="FormattedBody" IncrementViewCount="true" 
    runat="server" />       
    <p>&nbsp;</p>
<%
   1: Response.Write(String.Format
   2:  ("<a href=\"http://www.dotnetkicks.com/kick/?url=http://{0}\">" +
   3:     "<img src=\"http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://{0}\" border=\"0\" /></a>"
   4: , "aspxwizard.net" + CurrentWeblogPost.ViewPostURL));
%>

i went scanning for the tag that would render the body of the post and then added the raw <%%> block with a response.write that would display the URL, and it just worked :D

 

then i wanted to display the same button on the postlist.aspx ,and i did the same thing i looked for the tag that would render the body and added it like this

<CSBlog:WeblogPostData Property="FormattedBody" runat="server" />
 
<script language="javascript">
   1:  
   2:     var link = 'http://aspxwizard.net<CSBlog:WeblogPostData Property="ViewPostURL" runat="server" />';
   3:     document.write ('<p>&nbsp;</p><a href="http://www.dotnetkicks.com/kick/?url=' + link + '"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=' + link + '" border="0" /></a>')
</script>

 

this time i didn't know how to obtain the currentweblogpost instance so i introduced this post still rugged, but works fine and it took me 24 seconds :D

if u manage to do a better solution refer to this post URL and i'll create a track back for u and implement ur method :D

 

Update : this would also work for any other community button on community server blogs just user a different HTML like DIGG's code instead of dotnetkicks code

by amir.magdy | 1 comment(s)
Filed under: ,
Have u ever needed to extend a string? [Extension Methods]
Thursday, December 27, 2007 6:34 AM

well it's not inheritable by default so you can not extend it. that used to be the case before c# 3.0

now you can specify all the functionality that you need in an Extension Method. problem lies in that the extension method needs to access the instance of the object you are extending

well, it goes like this

1- first you declare a static class with ur static extension method an extension method is defined by the first parameter refering to the instance of the object it's going to extend using the Keyword this (note to implement your complicated algorithm to validate an Email :) )

   1: public static class StringExtension
   2: {
   3:     // note the use of this in the parameter
   4:     public static bool IsEmail(this string s){
   5:         if (s.IndexOf("@") > 0)
   6:         {