Javascript function launch under firefox

F

fcurvat

Hello,

I've got a little problem with <script></script> under firefox 1.0.7
If the script is not placed in the body tag, it isn't automaticaly
executed, but it is under Internet Explorer.

Here's an example of what i do.

Do anybody have any idea?

I am obliged to put the function in the <body onlaod=""> ?

Where can i find a specification of this kind of thing



<html>
<head>
<title></title>
<script language="javascript">

//////////////////////
function f_title ()
{
document.title='Test compatibility IE and FireFox.';
}

</script>


</head>
<body>

<table>

<tr>
<td>
Bas example : show the title
<a href="javascript:f_title ();">
GO
</a>

</td>
</tr>

<script language="javascript">
document.title='Test compatibility IE and FireFox.';
</script>

</table>

</body>
 
R

RobG

Hello,

I've got a little problem with <script></script> under firefox 1.0.7
If the script is not placed in the body tag, it isn't automaticaly
executed, but it is under Internet Explorer.

What I think you are saying is that if you try to modify the title by
running a script in the head element, and the title element has no
content or contains only whitespace, then Mozilla does not update the
title value.

However, Mozilla will update the value if the script is run after the
head finishes loading (i.e. is called from the body or onload).

So your choice is to:

- run it onload:
window.onload = f_title;

- put some innocuous character in the title element:
<title>-</title>

- run it from the head but use setTimeout to delay its execution
setTimeout('f_title()',0);


I think either of the first two is the best.

Here's an example of what i do.

Do anybody have any idea?

I am obliged to put the function in the <body onlaod=""> ?

Where can i find a specification of this kind of thing

document.title is documented at Mozilla and W3C (and likely Microsoft
too, but you're really asking about Mozilla, right?).

Mozilla.org:
<URL:http://developer.mozilla.org/en/docs/DOM:document.title>

W3C:
<UUURL:http://www.w3.org/TR/2000/WD-DOM-Level-2-HTML-20001113/html.html#ID-18446827>

If you are referring to the vagaries of browser behaviour... welcome to
the wonderful world of browser scripting!
<html>
<head>
<title></title>

If you put some content in the title element, you'll get what I expect
is your expected behaviour.
<script language="javascript">

The language is depreciated, type is required:

<script type="text/javascript">


[...]
 
F

fcurvat

OK, thanks for the tips.

THe real thing is, for me, to know why the scripts tags in the body are
executed under IE and not under Firefox. The "title" aspect of my
example is just ... an example.

Why is this script executed ? Is it random on the browser ?

<script language="javascript">
document.title='Test compatibility IE and FireFox.';
</script>
 
V

VK

OK, thanks for the tips.

THe real thing is, for me, to know why the scripts tags in the body are
executed under IE and not under Firefox. The "title" aspect of my
example is just ... an example.

Why is this script executed ? Is it random on the browser ?

<script language="javascript">
document.title='Test compatibility IE and FireFox.';
</script>

You're mixing direct statements execution and function declarations.
There is not any major difference here between browsers. Study this
sample (see also suggested format for pseudo-links):

<html>
<head>
<title>Direct statements vs. Functions</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="javascript">
// Executed immediately:
document.title='Foo';

// Waits for function call:
function changeTitle() {
document.title = 'Bar';
return false;
}
</script>
</head>

<body bgcolor="#FFFFFF">
<p><a href="noscript.html" onClick="return changeTitle()">Change
title</a></p>
</body>
</html>
 
R

RobG

OK, thanks for the tips.

THe real thing is, for me, to know why the scripts tags in the body are
executed under IE and not under Firefox. The "title" aspect of my
example is just ... an example.

Why is this script executed ? Is it random on the browser ?

<script language="javascript">
document.title='Test compatibility IE and FireFox.';
</script>

As VK says, in general there is no difference between browsers in this
regard. Scripts in the head (or anywhere else) are parsed and run as
they are encountered. You hit on a quirk of Firefox regarding the
title element - put the script before the title tags in the source and
all browsers will probably barf with an 'object not found' error.

In general, don't call any code that references, access or modifies an
element until that element's source has been parsed and and its object
added to the DOM.

Such scripts are usually placed after the elements they reference in
the HTML source (i.e. after the element's closing tag) or by calling
the functions/running statements using window.onload (or the body
onload attribute, which is essentially the same thing).


The classic case is something like:

<script type="text/javascript">
document.getElementById('blah').innerHTML = 'Found Blah';
</script>

<div id="blah"></div>


Which will almost certainly produce an error in all browsers. Put the
div before the script and all is fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top