I'm working on getting my javascript experience up a notch. I've got the basic Idea of what I want to do.
I want to make an automated javascript that calls to a php page.
This php page would generate an array with new notifications for it to display.
Now I need a push into the right direction, maybe some existing script to start with. I've been digging arround in jquery and prototype for ajax solutions but don't get much further.
My main questions:
How can I pull data from another page or file anyways?
In what format should the data be supplied?
Is there an existing script that I could st开发者_如何学编程art of?
Anyways, Thanks for your time!
How you want to display the notifications is op to you, but you could take a look at jQueryUI.
When it comes to the format, JSON is usually used because it's exactly the same as a javascript class/dict definition.
Existing scripts? not that I know of. But jQuery has plenty documentation of their built-in AJAX functions (Probably getJSON), and also for their UI components (Dialog is probably relevant).
Have a look at jQuery, it has a lot of features that you could also use in the future of your site.
How can I pull data from another page or file anyways?
using jQuery:
$.getJSON('ajax/test.json', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
non JSON approch
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
the 1st param would be the file you want to load, or get the context from,
secondly we pass in a function that would alert that the data was loaded, you can easily change this function to do what you want and display the data that was returned.
Query also offer a lot of documentation so whenever you're stuck just take a look at:
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/category/ajax/
In what format should the data be supplied?
I would suggest JSON, just because it works so easily and isn't as bloated as other structures. if you haven't worked with JSON before, checkout: http://www.json.org/js.html but a simple JSON structure would be:
{
"one": "Singular sensation",
"two": "Beady little eyes",
"three": "Little birds pitch by my doorstep"
}
Is there an existing script that I could start of?
jQuery should be more than enough: http://jquery.com/
精彩评论