Page 1 of 1

script problem

Posted: Sun Apr 24, 2011 1:19 pm
by Vegan
On my IT site I have been trying to get some code up to show how many days left before XP support expires

Here is the code

Code: Select all

	<script type="text/javascript">
 		if (top!= self) top.location.href = self.location.href;
 		function days_between(date1, date2) {
			// The number of milliseconds in one day
	    	var ONE_DAY = 1000 * 60 * 60 * 24;
    		// Convert both dates to milliseconds
    		var date1_ms = date1.getTime();
    		var date2_ms = date2.getTime();
	    	// Calculate the difference in milliseconds
    		var difference_ms = Math.abs(date1_ms - date2_ms);
        	// Convert back to days and return
	    	return Math.round(difference_ms/ONE_DAY);

		};
 		check_date(date1) {
	 		var now = new Date();
 		    if ((days_between(date1, now) < 1)
 		        document.write("expired");
 		    else
 		        document.write(days_between(date1, now), " days remaining");    
 		};

I invoke it with a script segment inside a table cell

Code: Select all

<td><script type="text/javascript">check_date(2014,4,8)</script></td>

Anyone see where I went wrong, at present the cell shows nothing

Re: script problem

Posted: Sun Apr 24, 2011 3:20 pm
by califdon
You aren't calling the function, as far as I can see.

[Edit: oops! I didn't even see your second "function" definition (the one without the keyword "function") because I'm used to the

Code: Select all

 tags, which have an {Expand] button. superdesign has already given you instructions, though. For future reference, when you're posting code here, try to use the appropriate code tags to make your posts as readable as possible.]

Re: script problem

Posted: Sun Apr 24, 2011 3:33 pm
by superdezign
Two things:
  1. You need to use the "function" keyword to define functions. So, you need to replace

    Code: Select all

    check_date(date1) {
    with

    Code: Select all

    function check_date(date1) {
    or

    Code: Select all

    check_date = function(date1) {
  2. You need to give that function a date. Right now,

    Code: Select all

    check_date(2014,4,8)
    is exactly the same as sending

    Code: Select all

    check_date(2014)
    because date1 in check_date is the only parameter you have. Instead of trying to send 3 parameters, try sending a Date object like so:

    Code: Select all

    check_date(new Date(2014,4,8));
    I'm sure you meant to, anyway.

Re: script problem

Posted: Sun Apr 24, 2011 4:10 pm
by Vegan
OK I changed the check_date to function as suggested
and I also changed the call to the form you suggested

still document.write should print something but its not

Code: Select all

		function days_between(date1, date2) {
			// The number of milliseconds in one day
	    	var ONE_DAY = 1000 * 60 * 60 * 24;
    		// Convert both dates to milliseconds
    		var date1_ms = date1.getTime();
    		var date2_ms = date2.getTime();
	    	// Calculate the difference in milliseconds
    		var difference_ms = Math.abs(date1_ms - date2_ms);
        	// Convert back to days and return
	    	return Math.round(difference_ms/ONE_DAY);

		};

Code: Select all

 		function check_date(date1) {
	 		var now = new Date();
 		    if ((days_between(date1, now) < 1)
 		        document.write("expired");
 		    else
 		        document.write(days_between(date1, now), " days remaining");
 		    return;    
 		};

Code: Select all

td><script type="text/javascript">check_date(new Date(2014,4,8));</script></td>

Re: script problem

Posted: Sun Apr 24, 2011 6:49 pm
by superdezign
Did you check your browser for JavaScript errors?

I see a syntax error.

Code: Select all

if ((days_between(date1, now) < 1)
One too many opening parentheses.

Re: script problem

Posted: Mon Apr 25, 2011 7:52 pm
by Vegan
Still not displaying anything

Re: script problem

Posted: Mon Apr 25, 2011 8:33 pm
by superdezign
superdezign wrote:Did you check your browser for JavaScript errors?
Doesn't sound like you're doing much debugging. Try taking the implicit suggestion that I gave, quoted above.

Re: script problem

Posted: Tue Apr 26, 2011 2:45 pm
by Vegan
I opened FF developer mode, seems it only was showing my developer page rather than the page in the window