Hello,

I am hoping one of you guru's can help me out. I am new to Javascript and a bit of a hack at HTML. The following works correctly in IE7 but doesn't work in Firefox. Any idea?

Also, is there any way to get IE to quit waring people of active content when they open this page with the annoying menubar thing?

An lastly is there a way to get the field to autopopulate when someone loads the page instead of the user having to click the button?

Thanks for any help you can give!
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Click here</title>
<script type="text/javascript">
function setTodayDate(id, offset) {
// dd/mm/yyyy
var date = new Date();
if(offset!==undefined)
date.setSeconds(date.getSeconds()+offset);
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var obj = document.getElementById(id);
if(obj)
obj.value = month+'/'+day+'/'+year;
}
</script>
</head>

<body>

<a href="#" onclick="setTodayDate('testfield', 2*24*60*60 );">Click here</a><br>
<input name="testfield" type="text" value size="12">

</body>

</html>

Recommended Answers

All 4 Replies

> The following works correctly in IE7 but doesn't work in Firefox. Any idea?

Because you are trying to retrieve the element using the id "testfield" but you don't have any element having an id "testfield", only one with a name "testfield". It works in IE because IE approximates or works with names if it doesn't find an element with a given ID. Form names are best accessed using the elements collection of the form object instead of searching for the element based on it's ID which is much slower. Read the Form access tutorial for more details.

> if(offset!==undefined)

Use if(typeof(offset) == 'undefined')) > <input name="testfield" type="text" value size="12"> <input name="testfield" type="text" size="12"> You don't need to specify an attribute if you are not using it and if you do, it needs to have a value, it can't be left blank. So it should be value="" .

> Also, is there any way to get IE to quit waring people of active content when they open
> this page with the annoying menubar thing?

It is some kind of weird security feature IE7 has implemented. I don't use IE but maybe this topic would be of some use to you.

> An lastly is there a way to get the field to autopopulate when someone loads the page
> instead of the user having to click the button?

Yes, attach a function to the onload event listener of the window object to do so. This function would then be fired as soon as the entire page loads.

<script type="text/javascript">
function init() {
  populate();
}

function populate() {
  var obj = document.forms[0].elements["testfield"];
  if(obj)
    obj.value = "hello all";
}

window.onload = init;
</script>

There are better ways of attaching handlers to listeners but this approach would do for the time being.

Thanks SOS! Your answers were VERY helpful!

~ Tyler

One last question...

How can I get the date to format like 03/24/2008 (month, Day year with exactly 2 digits for month and day and 4 digits for year.? It is almost right but there is no leading zero in the month and I am assuming probably the same for the day in the formula that I am using above.

Thanks again!

you can not get 03, you can change 3 to 03 through programming...do i need to tell how ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.