Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: JS onload gives some problems  (Read 2312 times)

brauck

  • Hero Member
  • *****
  • Karma: 18
  • Posts: 556
    • http://www.hbw-webdesign.nl/
JS onload gives some problems
« on: August 12, 2007, 11:04:37 pm »

By testing 1.6MU (good job Bob and contribs) I stumbled over a problem relating to the  JS event handler 'onload'.
In the (sNews) function js() this handler is used as follows:

Code: [Select]
// copy title in the sef url field
if (allowpreview == true) {window.onload = function() {window.self.setInterval("updatePreview()", 1500);};}
Now, when you use one (or more) other onload's after the one generated by sNews, ie. in the body-tag, obviously that one will override the one in the js-function which will lead to malfunctioning of the toggle-function of the preview-window in admin-mode (and of course visa versa).
So, to bypass this problem I made a little function that calls the needed functions after the page has loaded and put this in a external js-file, like this

Code: [Select]
function LoadFunctions() {
   function_1()
   function_2()
   function_3()
 }
window.onload = LoadFunctions;
Let's call the the external file: load.js and call it in the index.php just above the line <?php title();?> like this:

Code: [Select]
<script type="text/javascript" src="load.js"></script>
<?php title(); ?>
Now, when we are not in the admin mode everything is fine, but when we are, this onload handler will be over-ridden by the later assigned callback of the snews generated js.
To solve this I changed this little part of the function js() as follows:

Code: [Select]
// copy title in the sef url field
if (allowpreview == true) {
      window.onload = function() {
        window.self.setInterval("updatePreview()", 1500);
        LoadFunctions();
      };
}
Although this works it's not an elegant solucija IMHO, so I'm hoping for somebody who has more js-brains under his hat then I have.

Btw. it is more or less a browser problem too of course; Opera 9 handles everything without any hacks. I tested the above in IE6, IE7 and FF2.0 under XP and Vista as well.
« Last Edit: November 12, 2007, 02:02:16 pm by philmoz »
Logged
Confidence is reduced complexity.
brauck.nl for free css templates

Aysseline

  • Jr. Member
  • **
  • Karma: 4
  • Posts: 66
Re: JS onload gives some problems
« Reply #1 on: November 12, 2007, 01:01:54 pm »

I think that I've got a link to resolve multiple windows on load for js:
http://simonwillison.net/2004/May/26/addLoadEvent/

Like I'm not programmer or js's guru I don't really understand how to implemente this in sNews or in another js script, but I think that is the good way to solve the problem  ;D

Let me know if this work for sNews.
Logged
sNews 1.6_patched - localhost, Xampp 1.6.2 - XPSP2