Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opera

Status
Not open for further replies.

PcolaSteve

Programmer
Jul 1, 2011
30
0
0
US
I have a simple Javascript date code to display the current date on a web site. It works perfectly fine except in the Opera browser. It works perfectly fine in IE, Chrome, FF and Safari, but in Opera it doesn't show at all. I can not find any reason why it shouldn't be working in Opera.

Anyone have any ideas? I can supply the code if needed, but I thought that there may be a quirk with Opera I am not familar with.

The shortest distance between two points is NOT a straight line; it's through a worm hole!
 
Hi

In some situations Opera is more picky on standards than other browsers. If something so simple like displaying a date does has problem only in Opera, it generally indicates a syntax error in your code. So please post it. ( As possible not only the JavaScript but the related HTML too. )


Feherke.
 
Ok, here it is up to the onload command. I have edited out the meta tags and some of the other lines just for sake of clarity on this forum, so anything in {Edited Out} is just something edited out, this is not actually in the existing code.


<head>
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/>
<link href="mastercss.css" rel="stylesheet" type="text/css" />
<meta content="en-us" http-equiv="Content-Language" />

{Edited out}

<script type="text/javascript">
function startTime(){
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}
function checkTime(i){
if(i<10)
{
i="0" + i;
}
return i;
}
</script>

{Edited out} //Some CSS information

<script type="text/javascript">
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
</script>

<script type="text/javascript">
var mm=new Date();
mm=mm.getMonth();
switch(mm){

case (0):
var MON="January";
break;
case (1):
var MON="February";
break;
case (2):
var MON="March";
break;
case (3):
var MON="April";
break;
case (4):
var MON="May";
break;
case (5):
var MON="June";
break;
case (6):
var MON="July";
break;
case (7):
var MON="August";
break;
case (8):
var MON="September";
break;
case (9):
var MON="October";
break;
case (10):
var MON="November";
break;
case (11):
var MON="December";
break;
}

var dd=new Date(); //gets day
dd=dd.getDate();

var today=new Date(); //gets full year
today=today.getFullYear();
</script>
</head>

<body onload="startTime()" style="background-image: url('images/bg.gif'); background-repeat: repeat-x; margin-top: 12px;">


The shortest distance between two points is NOT a straight line; it's through a worm hole!
 
Hi

It works for me in Gecko, Presto and WebKit as soon as I added an element with [tt]id[/tt] "txt" : [tt]<div [maroon]id[/maroon][teal]=[/teal][green]"txt"[/green]></div>
[/tt] . My Opera is version 11.50 on Linux.

Regarding your code, I see nothing potentially wrong, but it looks really bad :
[ul]
[li]irrelevant name for the checkTime() function[/li]
[li]old fashion initialization of the weekday array[/li]
[li]horrible [tt]switch[/tt] on month[/li]
[li]instantiating 5 [tt]Date[/tt] objects while 2 would be enough[/li]
[li]using [tt]setTimeout()[/tt] for repetitive task instead of [tt]setInterval()[/tt][/li]
[li]passing the task to [tt]setTimeout()[/tt] as string instead of function[/li]
[/ul]
Honestly, I would just dump that JavaScript.


Feherke.
 
Thanks. I guess this is what I get when I use old data when working a script. I will redo it.

The shortest distance between two points is NOT a straight line; it's through a worm hole!
 
Hi

As an example, I would rewrite it like this :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]zf2[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]return[/b] [teal]([/teal]what[teal]<[/teal][purple]10[/purple][teal]?[/teal][green][i]'0'[/i][/green][teal]:[/teal][green][i]''[/i][/green][teal])+[/teal]what
[teal]}[/teal]

[b]function[/b] [COLOR=darkgoldenrod]showtime[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] now[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'txt'[/i][/green][teal]).[/teal]innerHTML[teal]=[/teal]
    now[teal].[/teal][COLOR=darkgoldenrod]getHours[/color][teal]()+[/teal][green][i]':'[/i][/green][teal]+[/teal]
    [COLOR=darkgoldenrod]zf2[/color][teal]([/teal]now[teal].[/teal][COLOR=darkgoldenrod]getMinutes[/color][teal]())+[/teal][green][i]':'[/i][/green][teal]+[/teal]
    [COLOR=darkgoldenrod]zf2[/color][teal]([/teal]now[teal].[/teal][COLOR=darkgoldenrod]getSeconds[/color][teal]())[/teal]
[teal]}[/teal]

window[teal].[/teal]onload[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal] [COLOR=darkgoldenrod]setInterval[/color][teal]([/teal]showtime[teal],[/teal][purple]500[/purple][teal])[/teal] [teal]}[/teal]
Regarding the weekday and month names, not sure if you intend to use them in the time displaying, but if yes, here is how I would do it :
JavaScript:
[b]var[/b] weekday[teal]=[[/teal][green][i]'Sunday'[/i][/green][teal],[/teal][green][i]'Monday'[/i][/green][teal],[/teal][green][i]'Tuesday'[/i][/green][teal],[/teal][green][i]'Wednesday'[/i][/green][teal],[/teal][green][i]'Thursday'[/i][/green][teal],[/teal][green][i]'Friday'[/i][/green][teal],[/teal][green][i]'Saturday'[/i][/green][teal]][/teal]
[b]var[/b] month[teal]=[[/teal][green][i]'January'[/i][/green][teal],[/teal][green][i]'February'[/i][/green][teal],[/teal][green][i]'March'[/i][/green][teal],[/teal][green][i]'April'[/i][/green][teal],[/teal][green][i]'May'[/i][/green][teal],[/teal][green][i]'June'[/i][/green][teal],[/teal][green][i]'July'[/i][/green][teal],[/teal][green][i]'August'[/i][/green][teal],[/teal][green][i]'September'[/i][/green][teal],[/teal][green][i]'October'[/i][/green][teal],[/teal][green][i]'November'[/i][/green][teal],[/teal][green][i]'December'[/i][/green][teal]][/teal]

[b]function[/b] [COLOR=darkgoldenrod]zf2[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]return[/b] [teal]([/teal]what[teal]<[/teal][purple]10[/purple][teal]?[/teal][green][i]'0'[/i][/green][teal]:[/teal][green][i]''[/i][/green][teal])+[/teal]what
[teal]}[/teal]

[b]function[/b] [COLOR=darkgoldenrod]showtime[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] now[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'txt'[/i][/green][teal]).[/teal]innerHTML[teal]=[/teal]
    weekday[teal][[/teal]now[teal].[/teal][COLOR=darkgoldenrod]getDay[/color][teal]()]+[/teal][green][i]' '[/i][/green][teal]+[/teal]
    now[teal].[/teal][COLOR=darkgoldenrod]getDate[/color][teal]()+[/teal][green][i]', '[/i][/green][teal]+[/teal]
    month[teal][[/teal]now[teal].[/teal][COLOR=darkgoldenrod]getMonth[/color][teal]()]+[/teal][green][i]' '[/i][/green][teal]+[/teal]
    now[teal].[/teal][COLOR=darkgoldenrod]getFullYear[/color][teal]()+[/teal][green][i]' '[/i][/green][teal]+[/teal]
    now[teal].[/teal][COLOR=darkgoldenrod]getHours[/color][teal]()+[/teal][green][i]':'[/i][/green][teal]+[/teal]
    [COLOR=darkgoldenrod]zf2[/color][teal]([/teal]now[teal].[/teal][COLOR=darkgoldenrod]getMinutes[/color][teal]())+[/teal][green][i]':'[/i][/green][teal]+[/teal]
    [COLOR=darkgoldenrod]zf2[/color][teal]([/teal]now[teal].[/teal][COLOR=darkgoldenrod]getSeconds[/color][teal]())[/teal]
[teal]}[/teal]

window[teal].[/teal]onload[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal] [COLOR=darkgoldenrod]setInterval[/color][teal]([/teal]showtime[teal],[/teal][purple]500[/purple][teal])[/teal] [teal]}[/teal]
By the way, if you still have problems with Opera, there must be a bug elsewhere, so you will have to show us the entire page with the problem.


Feherke.
 
Well, I re-coded the date issue to this:

<script type="text/javascript"> //gets current date
var current = new Date();
var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemember", "December");
var date = ((current.getDate() <10) ? "0" : "") + current.getDate();

function fullyear(number){
return (number < 1000) ? number + 1900 : number;
}

today = days [current.getDay()] + ", " + months [current.getMonth()] + " " + date + ", " + (fullyear(current.getYear()));
</script>

Still having an issue with Opera. I will work on it some more and if I can't find the solution, will follow-up. BTW, does this code look better? I think so; it looks cleaner too.

The shortest distance between two points is NOT a straight line; it's through a worm hole!
 
I figured out what was wrong with Opera. DA, (short for dumb-a**)here checked and Javascript was turned off. I need to fix this in the code as well to detect if script is running.

The shortest distance between two points is NOT a straight line; it's through a worm hole!
 
Hi

PcolaSteve said:
BTW, does this code look better?
Certainly looks better, excepting that fullyear() function. The [tt]Date[/tt] object has [tt]getFullYear()[/tt] method since JavaScript 1.3 ( that means 1998, the time of Netscape Navigator 4 and Explorer 4 ). No need to attempt compatibility with browsers older than that, even if the date trick will work, they will certainly fail elsewhere.
PcolaSteve said:
I need to fix this in the code as well to detect if script is running.
The best you can use is the [tt]noscript[/tt] tag. The content of [tt]noscript[/tt] is "processed" by hiding it, if script is disabled its content remains visible.


Feherke.
 
Yea, I was talking about adding the <noscript> tag.

Also, I am familiar witht he getFullYear() but for some reason didn't use it. I will correct this too.

I think one problem is that I am getting too many interruptions when I do this coding. I am not an expert and appreciate the help as I am still learning. I just wonder at time how I even made it thought C++ with a B+ average? LOL

The shortest distance between two points is NOT a straight line; it's through a worm hole!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top