Jump to content

Undead Javascript


delboy1978uk

Recommended Posts

Hi everyone! I have a page that loads html fragments into a <div> using AJAXWorks great! with one problem :-ssome of the html content runs some javascript. for instance one page has a scrolling news ticker, another has a highslide gallery.however when i link to another fragment, the contents of that div are no longer relevant, but the script continues to run!this means that, in the case of the news ticker, it looks for non existent elements and hence causes errorsin the case of the highslide gallery, if i load another page with a different gallery, two sets of controls appear!i need to be able to kill any running script before i load the next page up. Any ideas? ThanksDelboy

Link to comment
Share on other sites

Depends on how the script operates. If it's on a timeout or an interval, you can use clearTimeout() or clearInterval() respectively. If it acts upon a click, you'll have to wipe the nodes and the handlers at that point.

Link to comment
Share on other sites

Depends on how the script operates. If it's on a timeout or an interval, you can use clearTimeout() or clearInterval() respectively. If it acts upon a click, you'll have to wipe the nodes and the handlers at that point.
its an onclick, when i click my dynamic xml drop down, it fades out the container div, smoothly scrolls up the way, loads in the new html content, scrolls down, and fades back in.but the news ticker js keeps looking for the newsticker div which has gone! and my gallery pages (which use highslide btw) conflict with each other in that it tries to create a second instance of the div...so what do i need to do to wipe nodes and handlers? im quite new to javascript but omg its great, i have been missing out!
Link to comment
Share on other sites

You have to either keep track of the event handlers yourself and remove them when the element's they're dealing with are removed, or you can use something like removeEventListener to remove the listener if you're dealing with an actual event listener.http://www.howtocreate.co.uk/tutorials/javascript/domeventsFor something like the news ticker, you're going to need to manually detect when that is no longer needed and remove the interval that it's using to keep calling itself. Those modifications can probably be made to the news ticker code, when it gets the element that it's rotating you can add a check there to make sure the element exists before doing anything else. Basically, it's a bug in the news ticker to try to scroll an element that doesn't exist, they should have caught that when they made it. It's lazy not to look for errors like that.

Link to comment
Share on other sites

Lol! The ticker error has gone! So thanks!All i had to do was a simple encapsulation with this:if(document.getElementById("ticker")) { }As for the other issue, hmm... im not enough of an expert to know what to do.Have a look at the site in progress, http://www.ruthdales.co.uk/top_secret/home.htmlif you use the menu and choose either dogs, bitches, family, or exports from the dropdown, it takes you to a list of dogs it got from xmlclick a dog, and a summery panel loads into the main content area.the main image of the dog, if you click it, opens a highslide gallery so you can flick through images.looks great, appears to work fine....until you go and view some other dogs. then duplicate controls display at the top left of the web pagei think (dont quote me on this) that the highslide javascript is creating new instances of the gallery rather than using the same gallery.Anyone got any ideas then? Thanks in advance!delboy

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...