script problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

script problem

Post 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
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: script problem

Post 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.]
Last edited by califdon on Sun Apr 24, 2011 7:52 pm, edited 2 times in total.
Reason: I realized that I had overlooked your second "function" definition.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: script problem

Post 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.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: script problem

Post 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>
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: script problem

Post 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.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: script problem

Post by Vegan »

Still not displaying anything
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: script problem

Post 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.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: script problem

Post by Vegan »

I opened FF developer mode, seems it only was showing my developer page rather than the page in the window
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply