0

I am trying to work on something very simple in Javascript. I need the script to print out the date in the following format: yyyymmdd/yyyymmdd.

I just cant get the following script to work. It feeds a google calendar to output current Day view.

  var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();

    if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = yyyy+''+mm+''+dd;



    document.write ('<iframe src="https://www.google.com/calendar/embed?

        showTitle=0&amp;dates=today"/"today&amp;mode=DAY&amp;height=1200&amp;wkst=1&amp;hl=en_GB&amp;bgcolor

=%23FFFFFF&amp;src=my cal source&amp;color=%23711616&amp;ctz=Etc%2FGMT" style=" border:solid 

1px #777 " width="950" height="715"frameborder="0" scrolling="no"></iframe>');
Emil
  • 7,220
  • 17
  • 76
  • 135
Gerard
  • 43
  • 10

1 Answers1

4

The problem is string concatenation. You have to tell to JS what is a string and what is variable to add. Update your code as below:

document.write( '<iframe src="https://www.google.com/calendar/embed?showTitle=0&amp;dates='
    + today + '/' + today +
    '&amp;mode=DAY&amp;height=1200&amp;wkst=1&amp;hl=en_GB&amp;bgcolor=%23FFFFFF&src=my cal source&color=%23711616&ctz=Etc%2FGMT" style=" border:solid 1px #777 " width="950" height="715"frameborder="0" scrolling="no">' )
Onur Topal
  • 3,042
  • 1
  • 24
  • 41
  • I tried this but does not work still. – Gerard Nov 22 '12 at 16:51
  • @user1842944 If you're adding line breaks when defining a string you need to escape them with a `\ `. If still not working, check `document.write` is actually working as expected with something simple (and i.e. won't work in XHTML) – Paul S. Nov 22 '12 at 16:57
  • the date part is definitely working try replacing '&' to '&'. you test it here quickly http://jsfiddle.net/2XMFG/; – Onur Topal Nov 22 '12 at 16:59
  • the change from '&amp' to '&' worked perfectly... Onur TOPAL you are a life saver. May God Bless you man...and all you guys who tried to help me out here.. Thanks So much. However Opal could you please explain why I needed to get rid of the amp??...Once again THANKS!!! – Gerard Nov 22 '12 at 17:16
  • well; normally when you use the QueryString parameter you start with ? and continue with & for the others it how it is. You should only use & when you need encoded URL moslty in XML files. – Onur Topal Nov 22 '12 at 18:06