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
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:
- What is the simplest ajax upload plugin or script to be used with wordpress?Best solution by WordPress
- How to convert Oracle script to MySQL script?Best solution by Stack Overflow
- How to execute ajax script on Jquery mobile?Best solution by Stack Overflow
- What is the HTML code to make your web more accurate?Best solution by Yahoo! Answers
- How do you make a stable?Best solution by horses.about.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.