javascript - Best HTML structure for Ajax links -


when using javascript assign onclick handlers html <a> tags, have specific question when comes use of ajax - what's best way url ajax request? should use this.href or code handler function itself?

consider following link:

<a id="link" href="http://mysite.com/getdata.php">get data</a> 

then, javascript:

document.getelementbyid('link').onclick = function() {     // send ajax request } 

i using this.href inside handler i'm worried user clicks link before domready fires , links activated. better use href="javascript:void(0);" in html , construct url in handler itself? way, link nothing unless it's activated. what's best-practice here?

thanks, brian

think perspective: how page architecture hold if no js enabled?

your application should responsive user input under circumstances, , should not rely on implicit assumptions whether users have javascript-enabled browsers. sounds ridiculous, right? well, have plenty of clients work huge corps desktop configs locked down, everyone's running ie6 incredibly strict settings.

therefore, href attribute of anchor tag should point location want users go if not have javascript enabled (you should have fall-back in place ajax functionality same thing). in onclick handler should specify uri ajax call.

when it's said , done, users have js enabled (which majority) hit onclick handler in have uri ajax xhr call, , users not have js enabled redirected page achieves equivalent result.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -