Forum Moderators: open

Message Too Old, No Replies

calling func() from doc.write?

won't call

         

antipodes

2:39 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Is there a js programmer in the house?

It seems logical to me (as a beginning js writer) to call the function in an onload event, but it doesn't work.

Can someone tell me how to do this properly. It seems to be the only error in the code thus far.

I have to keep the code itself intact as it's a tech project.
thanks if u can help.....antipodes

<html>
<head>

<script Language="JavaScript">
<!-- hide

var timeStr, dateStr;

function clock() {
now= new Date();

// time
hours= now.getHours();
minutes= now.getMinutes();
seconds= now.getSeconds();
timeStr= "" + hours;
timeStr+= ((minutes < 10)? ":0" : ":") + minutes;
timeStr+= ((seconds < 10)? ":0" : ":") + seconds;
document.clock.time.value = timeStr;

// date
date= now.getDate();
month= now.getMonth()+1;
year= now.getYear();
dateStr= "" + month;
dateStr+= ((date < 10)? "/0" : "/") + date;
dateStr+= "/" + year;
document.clock.date.value = dateStr;

Timer= setTimeout("clock()",1000);
}

// -->
</script>
<script type="text/javascript" language="JavaScript">

<!--start hiding

function newWindow() {
var url="";
var name="popup";
var features="width=190,height=200,menubar=yes,status=yes";
myNewWindow=window.open(url,name,features);

myNewWindow.focus();
//give the new window focus
myNewWindow.document.open();
//open a document

myNewWindow.document.write("<html>");
myNewWindow.document.write("<head>");
myNewWindow.document.write("<title>Clock Displayer</title>");
myNewWindow.document.write("</head>");

myNewWindow.document.write("<body onLoad='clock()'>")

myNewWindow.document.write("<form name='display'>");
myNewWindow.document.write("<p>Time:</p>");
myNewWindow.document.write("<input type='text' name='time' size='16' value=''><br>");
myNewWindow.document.write("<p>Date:</p>");
myNewWindow.document.write("<input type='text' name='date' size='16' value=''>");
myNewWindow.document.write("</form>");
myNewWindow.document.write("<div align='right'>");
myNewWindow.document.write("<input type='button' value='EXIT' onClick='parent.close()'><br>");
myNewWindow.document.write("</div>");
myNewWindow.document.write("</body>");
myNewWindow.document.write("</html>");
myNewWindow.document.close();
}
//-->
</script>
</head>

<body bgcolor="#cccc99" text="#330000" link="navy" vlink="navy">
<h4>Topic 9<br>JavaScript ~ date object.</h4>
<hr color=#003300 noshade size=2>
<br>
<a href="JavaScript:newWindow('url','name','features')">Open Clock</A>
<br><br><br>
<hr color=#003300 noshade size=2>
</body>
</html>




</body>
</html>

korkus2000

2:49 pm on Mar 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this

myNewWindow.document.write("<body onLoad='window.opener.clock();'>")

I think your problem is the function is looking on page and not to the opener where your function is located.

antipodes

3:14 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Hi Korkus
Thanks 4 your reply but this didn't work either. IE5 (Mac) displayed an error msg about this is not an object or similar.

antipodes

korkus2000

4:01 pm on Mar 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you are referencing an object called clock which I don't see. You still need to reference the opener in the onload and add popup.document.display.fieldname to your reference calls. I think you want to change to this:

function clock() {
now= new Date();

// time
hours= now.getHours();
minutes= now.getMinutes();
seconds= now.getSeconds();
timeStr= "" + hours;
timeStr+= ((minutes < 10)? ":0" : ":") + minutes;
timeStr+= ((seconds < 10)? ":0" : ":") + seconds;
popup.document.display.time.value = timeStr;

// date
date= now.getDate();
month= now.getMonth()+1;
year= now.getYear();
dateStr= "" + month;
dateStr+= ((date < 10)? "/0" : "/") + date;
dateStr+= "/" + year;
popup.document.display.date.value = dateStr;

Timer= setTimeout("clock()",1000);
}