AJAX - Content Loading: Unterschied zwischen den Versionen
| Zeile 21: | Zeile 21: | ||
Variante wäre evtl. mit .innerHTML(); | Variante wäre evtl. mit .innerHTML(); | ||
==== Mit ajax() ==== | ==== Mit ajax() ==== | ||
| − | + | http://stackoverflow.com/questions/405409/use-jquery-selectors-on-ajax-loaded-html | |
| − | |||
| − | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
$.ajax({ | $.ajax({ | ||
| Zeile 34: | Zeile 32: | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | Klappt nicht richtig weil ajax einen String und kein Objekt zurückgibt. evtl, mal das checken: | ||
| + | // Finds all div elements within an XML document from an AJAX response. | ||
| + | $("div", xml.responseXML); | ||
| + | |||
| + | oder | ||
| + | <pre> | ||
| + | $.ajax({ | ||
| + | dataType : 'html', | ||
| + | url : 'path/to/example-page.html', | ||
| + | success : function(data) { | ||
| + | |||
| + | // set the returned contents in a new base <html> tag. | ||
| + | var response = $('<html />').html(data), | ||
| + | anchors, hrefValuesList = [ ], | ||
| + | i, end; | ||
| + | |||
| + | // now you can search the returned html data using .find(). | ||
| + | anchors = response.find('a'); | ||
| + | |||
| + | // grab all your href values from each anchor element. | ||
| + | end = anchors.length; | ||
| + | for (i = 0; i < end; i++) { | ||
| + | hrefValuesList.push( anchors[ i ].href ); | ||
| + | } | ||
| + | |||
| + | // continue processing the data as necessary... | ||
| + | |||
| + | } | ||
| + | }); | ||
| + | </pre> | ||
=== Funktion nach Abschluss des Ladevorgangs ausführen === | === Funktion nach Abschluss des Ladevorgangs ausführen === | ||
Version vom 2. März 2017, 15:10 Uhr
Links
- https://api.jquery.com/load/
- http://www.webdesignerdepot.com/2014/02/how-to-supercharge-your-sites-speed-with-ajax-and-jquery/ Interessant zu URL Hashes
- JavaScript - Page Transitions - Beispiele
Grundlagen
Strategien
Über AJAX ist es möglich nur Teile einer Website zu laden und an bestehende anzufügen oder diese zu ersetzen. Das spart Ladezeit. Aus SEO Sicht sollte man die URL ebenfalls aktualisieren. Und die Browserhistory aktualisieren. Außerdem sollte die gesamte Seite neu geladen werden, wenn Die Seite direkt im Browser aufgerufen wird. Also alle Inhalte.
Wenn Teile der Seite geladen werden sollen gibt es generell zwei Wege.
- Server checkt ob AJAX Anforderung vorliegt und liefert dann nur den benötigten Teil der Inhalte aus. I.d.R. den Inhalt.
- Über den AJAX Aufruf werden bestimmte Teile der Seite angefordert. Diese wird dann geparsed und die benötigten Inhalte geladen. Etwas mehr Ladevolumen, weil der komplette Markdown geladen wird. Allerdings nur die benötigten Verweise (also keine Skripte, Bilder, etc. die nicht benötigt werden).
- Evtl. gibt es interessante Möglichkeiten über serverseitigem JS ???
Load Container Content
Mit load()
Über jQuery den Inhalt eines Elements (inkl. Element) anfordern.
$("#target").load("something.html #source");
Oder nur die Inhalte ohne das umschließende DIV
$("#target").load("something.html #source > *");
Variante wäre evtl. mit .innerHTML();
Mit ajax()
http://stackoverflow.com/questions/405409/use-jquery-selectors-on-ajax-loaded-html
$.ajax({
url: 'something.html',
dataType: 'html',
success: function(html) {
var div = $('#sourceDiv', $(html)).addClass('done');
$('#targetDiv').html(div);
}
});
Klappt nicht richtig weil ajax einen String und kein Objekt zurückgibt. evtl, mal das checken:
// Finds all div elements within an XML document from an AJAX response.
$("div", xml.responseXML);
oder
$.ajax({
dataType : 'html',
url : 'path/to/example-page.html',
success : function(data) {
// set the returned contents in a new base <html> tag.
var response = $('<html />').html(data),
anchors, hrefValuesList = [ ],
i, end;
// now you can search the returned html data using .find().
anchors = response.find('a');
// grab all your href values from each anchor element.
end = anchors.length;
for (i = 0; i < end; i++) {
hrefValuesList.push( anchors[ i ].href );
}
// continue processing the data as necessary...
}
});
Funktion nach Abschluss des Ladevorgangs ausführen
Beispiel
$("button").click(function(){
$("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
});
Beispiel
$( "#success" ).load( "/not-here.php", function( response, status, xhr ) {
if ( status == "error" ) {
var msg = "Sorry but there was an error: ";
$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
}
});
Beispiel
function loadContent(url,selector){ // Load content
var pageData = '';
$.ajax({
type: "POST"
url: url,
data: {
ajax: true
},
success: function(data,status,xhr){
if(statusTxt == "error"){
$pageData = '<div class="error">Sorry - something went wrong</div>';
}else {
$pageData = data;
}
}
}).done(function(){
// actions after complete loading
}
});