[TYPO3-english] Javascript in HTML template

"Christian Müller (Kitsunet)" christian at kitsunet.de
Thu Dec 2 20:24:11 CET 2010


Have you tried to open this html from the server (without TYPO3 
involved)? I guess it won't work too, because actually you execute your 
javascript right away, without checking if all of the page is already 
loaded. Which can be fine on a local pc because there is no loading 
time, but from the internet the html takes a bit to load and your 
javascript is executed before the img tag with id="someimage" exists in 
your browser and so you will get the error.

You should put that code into a function and execute that function with 
window.onload [1].

Christian

[1] http://javascript.about.com/library/blonload.htm

On 01.12.2010 09:54, Cecilie Andersen wrote:
> I have a problem regarding a Javascript in my html template.
>
> When I run the template on my local pc it works, but when loading the
> template on my typo3 site there is an error.
>
> The javascript is the following (it just insert a random picture into a
> <td>
>
> <script type="text/javascript">
> var aryimages = new Array('banner2.jpg');
> document.getElementById("someimage").src =
> aryimages[Math.floor(Math.random() * aryimages.length)];
> </script>
>
> </HEAD>
>
> <body>
>
> <div align="center">
> <table border="0" style="border: 1px solid #319a63" width="956"
> cellspacing="0" cellpadding="0" height="660">
> <tr>
> <td valign="bottom" width="956" height="153" id="menu" colspan="3"><img
> id="someimage" src="">menu</td>
> </tr>
>
>
> When opening the page in typo3 I can see an explorer error, that calls
> the following error:
>
> 'document.getElementById(...)' is null or not an object
>
> - and it points to the line "document.getElementById("someimage").src =
> aryimages[Math.floor(Math.random() * aryimages.length)];"
>
> Anyone has knowledge about problem with inserting javascript ? as it
> seems to be an typo3 related error, as it works fine on my local pc.
>
> Regards
> Cecilie



More information about the TYPO3-english mailing list