My previous blog entry has a link to another site. It is not just a link but is actually a form that is submitted to the other site. This caused an interesting problem during implementation.

These blog entries have HTML enabled so I can do some formatting and also so that I can include a link to my own personal CSS file. I need to do this because I opted to use the generic blog feature of the 1&1 web server and I can’t change the CSS or JavaScript for the blog directly. Each entry gets a link tag added to it to link to my own CSS file on my own website.

The problem with the embedded form and the JavaScript to go with it is that the script tag is enclosed in paragraph tags and doesn’t work. It looks okay but the browser doesn’t seem to accept it as script in that situation.

The solution was to place all of the JavaScript code inside of the onlick attribute for the hyperlink that submits the form. I don’t use a button in the form because I didn’t want a button. Where this got tricky was how the function is specified. I used the new function() {} syntax and placed all of the code in that function. It must be all on a single line up to the end of the closing of the hyperlink tag or the blog software would add line break tags inside of the JavaScript.

The form in the blog post looks like this:

<form method="post" target="-blank">
<div style="text-align: center;">
<a href="#" onclick='new function() { var form1 = document.forms[0]; today = new Date(); var themonth =  today.getMonth() + 1;	var theyear = today.getYear(); if (theyear < 1000) { theyear += 1900; } var theday = today.getDate() - 1; var datestring = themonth+ "/" + theday+ "/" + theyear; var theaction = ""; theaction  += "startdate=" + datestring + "&" + "enddate=" + datestring + "&";  form1.action = theaction;  form1.submit(); return false; };'><b>Click Here</b></a>

You can also go to that post and view the page source if you want to see it in some sort of editor.