Is it possible to change a script when it is running?

Are there any solutions to make my ajax script stable regardless of HTML changing?

  • I'm running a content-based website, and I usually used ajax to dynamically add items to the content list. Every time I updated my item structure I have to change my javascript to fit the new structure. I wonder whether there was any solution to keep script stable regardless of the changing of HTML?

  • Answer:

    Make sure you are using proper, semantic CSS.  That way you can target the part of the HTML you really want to target rather than moving up and down the DOM tree.  E.g. instead of doing: <div class=blog> <div class=post> content of most recent post </div> <div class=post> content of older post </div> <div class=post> content of even older post </div></div> you should be doing something like: <div class=blog id=blog-1> <div class=post id=post-most-recent> content of most recent post </div> <div class=post id=less-recent> content of older post </div> <div class=post id=least-recent> content of even older post </div></div> Ideally you have code that does this for you (e.g. HAML for Ruby on Rails will automatically do this for you by appending identifiers to the id names so you wold get post-1, post-2 etc.) The point is that you then easily get to the ID you want rather than going from a child tag to its parent, which will break if you introduce a grand parent,  grand child or siblings.

Mark Vilrokx at Quora Visit the source

Was this solution helpful to you?

Other answers

A possible solution would be to use Javascript templating, you could try using John Resig's micro templating engine, here: http://ejohn.org/blog/javascript-micro-templating/This way you would separate the item template from the page.

Diego Trinciarelli

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.