www.webdeveloper.com
Page 9 of 15 FirstFirst ... 7891011 ... LastLast
Results 121 to 135 of 221

Thread: Here are 2 prototype-functions I created.

  1. #121
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Code:
    String.prototype.stripHTML=function(){
    return this.replace(/\&/g,"\&amp;").replace(/\</g,"\&lt;").replace(/\gt/g,"\&gt;")
    }
    Converts "<b>hi</b>" into "&lt;b&gt;hi&lt;/b&gt;"

  2. #122
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by Ultimater
    BigMoosie, can you share the changes you made to it?
    Its not quite efficient enough. But ill share how it scrolls the colors:

    #FF0000
    #FF00FF
    #0000FF
    #00FFFF
    #00FF00
    #FFFF00


    But it also has a variable to control how light it gets.

  3. #123
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Date.prototype.NumberOfDays(); Origional: A1ien51

    I was modifying the old version to maintain the old date when I started from scratch. As much as I like the old one for its novel approach, I prefer this as it is 10 times faster.

    Code:
    Date.prototype.NumberOfDays=function(){
    	var a=this.getMonth();
    	if (a==1) {
    		var b=this.getFullYear();
    		return (b%4==0 && (b%100!=0 || b%400==0)) ? 29 : 28;
    	}
    	return (a==3 || a==5 || a==8 || a==10) ? 30 : 31;
    }
    Last edited by BigMoosie; 06-08-2005 at 02:58 AM.

  4. #124
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Moosie, isn't the rule for leap years:

    Code:
    if(year%4==0 &&(!year%100==0 || year%400==0)) //then leap year
    ?

    Source: http://www.timeanddate.com/date/leapyear.html
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  5. #125
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Good point HaganeNoKokoro!

    I knew that they occasionally make exceptions for leapyears but I didnt know there was a rule for it, I thought that chaos didnt allow for those kind of accurate predictions, it seems I was wrong. You can make that adjustment if you want. But for most cases it will not matter as it wont be effective in roughly a hundred years in both directions.

    EDIT: made the correction anyway.
    Last edited by BigMoosie; 06-08-2005 at 02:38 AM.

  6. #126
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Boolean.isTrue();

    Code:
    Boolean.prototype.isTrue=function(){return this;}
    This may come in handy
    Last edited by BigMoosie; 06-08-2005 at 03:01 AM.

  7. #127
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Wow, I've needed a function like this for so long...
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  8. #128
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Quote Originally Posted by BigMoosie
    Boolean.isTrue();

    Code:
    Boolean.prototype.isTrue=function(){return this;}
    This may come in handy
    I sure hope you are kidding!
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  9. #129
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Quote Originally Posted by HaganeNoKokoro
    Wow, I've needed a function like this for so long...
    I think he's being sarcastic

    BigMoosie, as A1ien51 points-out, the function doesn't do anything and it's a wasted function call.

    Code:
    Boolean.prototype.isTrue=function(){return this;}
    A boolean is either true or false, so your function call might look like this:

    alert(true.isTrue())//true
    alert(false.isTrue())//false

    Maybe you meant something like this:
    Code:
    Boolean.prototype.ifElse=function(){if(this==true)return arguments[0];else return argument[1]}
    Last edited by Ultimater; 06-08-2005 at 01:54 PM.

  10. #130
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Quote Originally Posted by A1ien51
    I sure hope you are kidding!
    Definitely kidding, and I'm pretty sure Moosie is too
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  11. #131
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by Ultimater
    Maybe you meant something like this:
    Code:
    Boolean.prototype.ifElse=function(){if(this==true)return arguments[0];else return argument[1]}
    Of course I'm being sarcastic fool! and I meant exactly what I wrote, even yours is a ridiculous waste of a function because it is equivalent to:

    Code:
    (myBoolean) ? "arg0" : "arg1";

  12. #132
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Any Boolean prototype-function is a joke! -- even mine

    I'm stumped on why JavaScript even has Boolean prototypes in the first place.

  13. #133
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    cos that way no-one complains about them not being there.

    and anyway booleans are great.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  14. #134
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    here is two more date prototypes UK to US format and DaysBetween, I do not think I posted these.

    Code:
    <script type="text/javascript">
    
      Date.prototype.DaysBetween = function(){
        var intMilDay = 24 * 60 * 60 * 1000;
        var intMilDif = arguments[0] - this;
        var intDays = Math.floor(intMilDif/intMilDay);
        return intDays;
      }
    
      String.prototype.UKtoUSDate = function(){
        var arrDate = this.split("/");
        return new Date(arrDate[1] + "/" + arrDate[0] + "/" + arrDate[2]);
      }
    
      var d1 = new Date("06/10/2005");
      var d2 = new Date("06/13/2005");
      alert(d1.DaysBetween(d2));
    
      var dUK1 = ("10/06/2005").UKtoUSDate();
      var dUK2 = ("13/06/2005").UKtoUSDate();
      alert(dUK1.DaysBetween(dUK2));
    
    </script>
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  15. #135
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Business Day Calcs, this method does not involve looping like I have seen people do.

    Code:
    Date.prototype.DaysBetweenBusiness = function(){
      //Does not account for holidays! LOL
    
      //return -1 if start or end date is not a business day
      if(!this.IsWeekDay() || !arguments[0].IsWeekDay())return -1
     
      var intMilDay = 24 * 60 * 60 * 1000;
      var intMilWeek = intMilDay * 7;
      var intSDay = this.getDay();
      var intEDay = arguments[0].getDay();
      this.setDate(this.getDate() + 7 - intSDay);
      arguments[0].setDate(arguments[0].getDate() - intEDay);
      var intMilDif = arguments[0] - this;
      var intDaysTotal = Math.floor(intMilDif/intMilDay);
      var intWeeks = Math.floor(intMilDif/intMilWeek);
      return xD = 5 - intSDay + intEDay + intDaysTotal -(intWeeks*2)
    }
    
    Date.prototype.IsWeekDay = function(){
      return this.getDay() > 0 && this.getDay() < 6
    }
    
      var d1 = new Date("06/6/2005");
      var d2 = new Date("06/29/2005");
      alert(d1.DaysBetweenBusiness(d2));
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

Thread Information

Users Browsing this Thread

There are currently 5 users browsing this thread. (0 members and 5 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles