Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33567
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Getting text with AJAX |
| Date | 2017-07-01 12:10 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <871sq09zvj.fsf@bsb.me.uk> (permalink) |
| References | <31a6bc47-820d-4029-9ff1-c46617cf7e6f@googlegroups.com> |
bit-naughty@hotmail.com writes:
> ...OK, so assume there's some text on the screen, like a headline, or
> topic, say. When it's clicked, the actual text has to be got (from the
> server), and shown.
> What would the coding look like?
> (assume the text itself is on a MySQL db on the server, with some kind
> of "id" to it, which will be fetched by a PHP script -
> ie. www.site.com/gettext.php?articleid=210).
One possible outline:
1. The document could associate the ID with the headline using a data-*
attribute:
<h1 data-article-id=210>Usenet not dead despite rumours</h1>
2. An click event handler is then associated with every headline. I
usually do this on load:
window.addEventListener('load', function () {
var headlines = document.querySelectorAll('h1[data-article-id]');
for (var i = 0; i < headlines.length; i++)
headlines[i].addEventListener('click', ...);
}, false);
3. That event handler constructs the required URL either by extracting
the ID from the event object (say via the target attribute).
4. It then creates an XMLHTTPRequest for that resource whose 'load'
handler will do whatever you want with the result.
It's all a bit vague because there are so many ways to handle the
details and very little in your question to guide an answer.
--
Ben.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Getting text with AJAX bit-naughty@hotmail.com - 2017-07-01 02:49 -0700 Re: Getting text with AJAX Luuk <luuk@invalid.lan> - 2017-07-01 11:56 +0200 Re: Getting text with AJAX Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-07-01 12:10 +0100 Re: Getting text with AJAX JJ <jj4public@vfemail.net> - 2017-07-01 21:35 +0700 Re: Getting text with AJAX JR <jrfaqcom@gmail.com> - 2017-07-02 13:37 -0300
csiph-web