469,600 Members | 2,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Javascript function launch under firefox

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>

Oct 25 '05 #1
4 1844
fc*****@gmail.com wrote:
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">
[...]
--
Rob
Oct 25 '05 #2
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>

Oct 25 '05 #3
VK

fc*****@gmail.com wrote:
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>

Oct 25 '05 #4
fc*****@gmail.com wrote:
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.
--
Rob
Oct 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Joe | last post: by
2 posts views Thread by Cy | last post: by
18 posts views Thread by marcokrechting | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.