Link to home
Start Free TrialLog in
Avatar of jack_p50
jack_p50

asked on

Updated since last visit

I saw 1 site that was topic What's updated since your last visit. How to do it (i think it's Javascript)
Avatar of mouatts
mouatts

The chances are that it isn't done by javascript but by the server. When you connect to this site they will be sending you a cookie that will contain todays date. When you next connect this cookie is sent back to them and the server simply has to look in its database of changes for those implemented since the date in the cookie.

Steve
I think mouatts is right. It could be done by JavaScript, but server-side would be so much easier.

Martin
Avatar of jack_p50

ASKER

That's very good, but how to do this?
Is this ok?

<HTML>
<HEAD>
<TITLE>What's new?</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
// Initialization
lastVisit = getCookie("Last visit");
if (lastVisit == null) lastVisit = 0;
today = new Date();
setCookie("Last visit", today.getTime());
descriptions = new Array();
dates = new Array();

// List all updates
// To get the integer that you should use, open up Netscape and write
// mocha:getCode()
addUpdate("Bug fixes", 914280388850)
addUpdate("Minor improvements", 914280529020)

// Function declarations
function updates() {
  s = "";
  for (i=0;i<dates.length;i++) {
    if (dates[i] > lastVisit) {
      s += "<B>" + new Date(dates[i]) + "</B><BR>" + descriptions[i] + "<P>";
    }
  }
  if (s == "")
    return "Nothing has happened around here since the last time you were here.";
  else
    return "This has happened since your last visit:<P>" + s;
}
function setCookie(name, value, expire) {
  document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : (";expires=" + expire.toGMTString()));
}
function getCookie(Name) {
  var search = Name + "=";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }
  }
}
function addUpdate(description, date) {
  descriptions[descriptions.length] = description;
  dates[dates.length] = date;
}
function getCode() {
  d = new Date();
  prompt("Here's the code:", 'addUpdate("...", ' + d.getTime() + ')');
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
  document.write(updates());
// -->
</SCRIPT>
</BODY>
</HTML>

I can write a date format routine to get it look more "professional". Just tell me how you want it to look.

Martin
Ok, please do it and send as answer. Exactly I want :
1)User came to site and visit all updates
2)He comes again and visits "Since your last visit, was updated ....."(list of updates as you did).
3)Please, do such thing that if user wants, he can see all updates (i.e. some button "see all updates")
4)Big thanx  ;)
I'm no good at date formats, but this is how I've made it: "Tuesday 22 Dec, 18:09" Let me know if it's wrong and I'll change it.

<HTML>
<HEAD>
<TITLE>What's new?</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
// Initialization
last = getCookie("Last visit");
if (last == null) last = "0";
lastVisit = parseInt(last);
lastVisit *= 1000;

today = new Date();
expdate = new Date();
expdate.setTime(expdate.getTime() + 30*24*60*60*1000);
setCookie("Last visit", today.getTime()/1000, expdate);
descriptions = new Array();
dates = new Array();

// List all updates
// To get the code that you should use, open up Netscape and write
// mocha:getCode()
// In the location field
addUpdate("Bug fixes", 914280388850);
addUpdate("Minor improvements", 914280529020);
addUpdate("Date formatting and allUpdates() function", 914342950000);

// Function declarations
function allUpdates() {
  if (dates.length == 0)
    code = "The list of updates is currently empty.";
  else {
    code = "<TITLE>All updates</TITLE>";
    for (i=0;i<dates.length;i++)
      code += "<B>" + formatDate(dates[i]) + "</B><BR>" + descriptions[i] + "<P>";
    code += "<P><HR>There have been <B>" + dates.length + "</B> updates.";
  }
  w = open('','','height=300,width=300,scrollbars');
  w.document.open();
  w.document.write(code);
  w.document.close();
}
function updates() {
  s = "";
  for (i=0;i<dates.length;i++) {
    if (dates[i] > lastVisit)
      s += "<B>" + formatDate(dates[i]) + "</B><BR>" + descriptions[i] + "<P>";
  }
  if (s == "")
    return "Nothing has happened around here since the last time you were here.";
  else
    return "This has happened since your last visit:<P>" + s;
}
function setCookie(name, value, expire) {
  document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : (";expires=" + expire.toGMTString()));
}
function getCookie(Name) {
  var search = Name + "=";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }
  }
}
function addUpdate(description, date) {
  descriptions[descriptions.length] = description;
  dates[dates.length] = date;
}
function getCode() {
  d = new Date();
  prompt("Here's the code:", 'addUpdate("...", ' + d.getTime() + ')');
}
function formatDate(date) {
  d = new Date(date);
  // hh:mm
  h = d.getHours();
  m = d.getMinutes();
  time = ((h<10) ? "0"+h : h) + ":" + ((m<10) ? "0"+m : m);
  // month
  months = new Array(12);
  months[0] = "Jan"; months[1]  = "Feb"; months[2]  = "Mar";
  months[3] = "Apr"; months[4]  = "May"; months[5]  = "Jun";
  months[6] = "Jul"; months[7]  = "Aug"; months[8]  = "Sep";
  months[9] = "Oct"; months[10] = "Nov"; months[11] = "Dec";
  month = months[d.getMonth()];
  // weekday
  days = new Array(7);
  days[0] = "Sun"; days[1] = "Mon"; days[2] = "Tues"; days[3] = "Wednes";
  days[4] = "Thurs"; days[5] = "Fri"; days[6] = "Satur";
  weekday = days[d.getDay()] + "day";
  return weekday + " " + d.getDate() + " " + month + ", " + time;
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
  document.write(updates());
  document.write('<FORM><INPUT TYPE="BUTTON" VALUE="All updates" onClick="allUpdates()"></FORM>');
// -->
</SCRIPT>
</BODY>
</HTML>

Martin
WoW!
Here are last questions :
1)Please do in so format : "22th of December, 1998 :
                                         ..............."
2)Please do when user presses all updates it reloads to current window.
3)Next time post it as answer

Thanx
and please, in "there were ... updates" add "since 2 december, 1998"(i.e.)
<HTML>
<HEAD>
<TITLE>What's new?</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
// Initialization
last = getCookie("Last visit");
if (last == null) last = "0";
lastVisit = parseInt(last);
lastVisit *= 1000;

today = new Date();
expdate = new Date();
expdate.setTime(expdate.getTime() + 30*24*60*60*1000);
setCookie("Last visit", today.getTime()/1000, expdate);
descriptions = new Array();
dates = new Array();

// List all updates
// To get the code that you should use, open up Netscape and write
// mocha:getCode()
// In the location field
addUpdate("Bug fixes", 914280388850);
addUpdate("Minor improvements", 914280529020);
addUpdate("Date formatting and allUpdates() function", 914342950000);

// Function declarations
function allUpdates() {
  if (dates.length == 0)
    code = "The list of updates is currently empty.";
  else {
    code = "<TITLE>All updates</TITLE>";
    for (i=0;i<dates.length;i++)
      code += "<B>" + formatDate(dates[i]) + "</B><BR>" + descriptions[i] + "<P>";
    code += "<P><HR>There have been <B>" + dates.length + "</B> updates since " + formatDate(dates[0]) + ".";
  }
  location.href = "write.htm?" + escape(code);
}
function updates() {
  s = "";
  for (i=0;i<dates.length;i++) {
    if (dates[i] > lastVisit)
      s += "<B>" + formatDate(dates[i]) + "</B><BR>" + descriptions[i] + "<P>";
  }
  if (s == "")
    return "Nothing has happened around here since the last time you were here.";
  else
    return "This has happened since your last visit:<P>" + s;
}
function setCookie(name, value, expire) {
  document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : (";expires=" + expire.toGMTString()));
}
function getCookie(Name) {
  var search = Name + "=";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }
  }
}
function addUpdate(description, date) {
  descriptions[descriptions.length] = description;
  dates[dates.length] = date;
}
function getCode() {
  d = new Date();
  prompt("Here's the code:", 'addUpdate("...", ' + d.getTime() + ')');
}
function formatDate(date) {
  d = new Date(date);
  // hh:mm
  h = d.getHours();
  m = d.getMinutes();
  time = ((h<10) ? "0"+h : h) + ":" + ((m<10) ? "0"+m : m);
  // month
  months = new Array(12);
  months[0] = "January"; months[1]  = "February"; months[2]  = "March";
  months[3] = "April"; months[4]  = "May"; months[5]  = "June";
  months[6] = "July"; months[7]  = "August"; months[8]  = "September";
  months[9] = "October"; months[10] = "November"; months[11] = "December";
  month = months[d.getMonth()];
  // day
  day = d.getDate() + "";
  last = day.substring(day.length-1);
  if (last == "1") day += "st";
  else if (last == "2") day += "nd";
  else if (last == "3") day += "rd";
  else day += "th";
  return day + " of " + month + ", " + (d.getYear()+1900);
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
  document.write(updates());
  document.write('<FORM><INPUT TYPE="BUTTON" VALUE="All updates" onClick="allUpdates()"></FORM>');
// -->
</SCRIPT>
</BODY>
</HTML>

You will also need write.htm:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
if (location.search && location.search.length > 0)
  document.write(unescape(location.search.substring(1)));
// -->
</SCRIPT>
</HEAD>
</HTML>

Merry Christmas, Martin
Very sorry, but I have forgotten one thing :
1)what's mocha:getcode() and how to use it (nothing happens when I write it in location)
2)how to calculate update's time (in addupdate() )
3)can you please do it to look as that page : "www.geocities.com/TimesSquare/Portal/9206/news.html"
4)when I press allupdates button, all is ok, but there's very many **** in location bar.
I appreciate your great efforts for so few points - sorry, I'm lack of them
Can allupdates be done without write.htm?
Please, do it, I pray that it's last thing that I ask about it - I'll accept your next answer.
BIG thanx to you.
1) what's mocha:getcode() and how to use it (nothing happens when I write it in location)
Are you using Netscape? BTW, it should be
  mocha:getCode()

2)how to calculate update's time (in addupdate())
  mocha:getCode()
will give you the code to copy and paste it in.

3)can you please do it to look as that page: "www.geocities.com/TimesSquare/Portal/9206/news.html
Here are the two functions that are changed:
function allUpdates() {
  if (dates.length == 0)
    code = "The list of updates is currently empty.";
  else {
    code = "<TITLE>All updates</TITLE>";
    for (i=0;i<dates.length;i++)
      code += "<H3>Update at " + formatDate(dates[i]) + " :</H3>\n<BLOCKQUOTE>" + descriptions[i] + "\n</BLOCKQUOTE>";
    code += "<P><HR>There have been <B>" + dates.length + "</B> updates since " + formatDate(dates[0]) + ".";
  }
  location.href = "write.htm?" + escape(code);
}
function updates() {
  s = "";
  for (i=0;i<dates.length;i++) {
    if (dates[i] > lastVisit)
      s += "<H3>Update at " + formatDate(dates[i]) + " :</H3>\n<BLOCKQUOTE>" + descriptions[i] + "\n</BLOCKQUOTE>";
  }
  if (s == "")
    return "Nothing has happened around here since the last time you were here.";
  else
    return "This has happened since your last visit:<P><CODE>" + s + "<P></CODE>";
}

4)when I press allupdates button, all is ok, but there's very many **** in location bar.
Can allupdates be done without write.htm?
It can be done in another way, but then you'll have to use cookies. Since cookies can be turned of there's a risk that nothing will show up. Do you want me to implement them anyway (if they don't accept them it's their problem, right?:-))?
There will still be a redirection to another page (not neccesarily write.htm, it could be allupdates.htm or something like that).

Merry Christmas, Martin
Ok, but I still don't know how to use mocha:getCode() (it doesn't does nothing)
Can you give me example of using it?
Btw, I use Netscape 4.5
Also, please add <code> to place where <blockquote> is.
Can you PLEASE do so that it would be almost identical to my page.
Big thanx to you, you're great guy to deal with!
Ok, mocha:getCode() works, but does it return current time? Can it return any time (i.e. mocha:getCode(22,12,1998) which stands for 22th of December, 1998). Thanx one more time.
It looks just like your home page already.

Here's the new getCode():
function getCode(d, m, y) {
  if (y && y < 1900) y += 1900;
  k = new Date();
  params = (y ? y : k.getYear()) + ',' + (m ? m-1 : k.getMonth()) + ',' + (d ? d : k.getDate());
  eval("a = new Date(" + params + ")");
  prompt("Here's the code:", 'addUpdate("...", ' + a.getTime() + ')');
}

Martin
Ok, I'm satisfied. Thanx and answer this q
ASKER CERTIFIED SOLUTION
Avatar of martinag
martinag

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
At last it's done just superb