views:

316

answers:

2

If I load this in a new tab (FF3) it works. If I load it in the current tab it just prints the url.

I think it's actually a matter of loading it in an existing gmail tab. So say, make the bookmarklet, click it once, then click it again. That seems to be the way to recreate the problem.

Any idea what would cause this? I can think of a few workarounds, but I'm curious about how this works.

javascript:var%20t=new%20Date(),y=t.getFullYear(),m=t.getMonth()+1,d=t.getDate();document.location.href="http://mail.google.com/mail/#search/is%3Aunread+after%3A"+y+"-"+m+"-"+d

/* same code split up for readability */
javascript:
  var t = new Date(),
      y = t.getFullYear(),
      m = t.getMonth()+1,
   /* d = t.getDay(); I actually have this correct above, but not here.. oops */
      d = t.getDate(); 
  document.location.href="http://mail.google.com/mail/#search/is%3Aunread+after%3A"+y+"-"+m+"-"+d;

Any help?

Thanks :)

UPDATE:

when I remove the extra whitespace from this answer and covert the necessary spaces to "%20" (url encoding), it does nothing at all:

 /* this works. I was missing the final ")" altCognito wrote */
 javascript:void((function(){var%20t=%20new%20Date(),y=t.getFullYear(),m=t.getMonth()+1,d=t.getDate();window.location.href="http://mail.google.com/mail/#search/is%3Aunread+after%3A"+y+"-"+m+"-"+d;})())

I also experimented with some semicolon fiddling and other general syntax checks, but I'm not sure what I'm looking for. It works neither as a bookmarklet or when pasted straight into the address bar (for me anyway).

A: 

It is better practice to use window.location.href instead of document.location

epascarello
I don't think that was it. I updated the question with new info. Thanks!
ThomasGHenry
+3  A: 

What you want is something that looks like this:

javascript:void(
    (function() {
    var t = new Date(),
          y = t.getFullYear(),
          m = t.getMonth()+1,
          d = t.getDate();
      window.location.href="http://mail.google.com/mail/#search/is%3Aunread+after%3A"+y+"-"+m+"-"+d;
    })()
)

The key is the void((function() {... Your stuff here ... })())

Note, that you also want to use getDate(), not getDay, as getDay returns the day of the week!

altCognito
what is the meaning of this change anyway? why the void and the anonymous function call?
ThomasGHenry