Szukam kawalka kodu (AJAX) ktory obsuzy mi powiadomienia na stronie tak, ze bedzie w petli powiedzmy co 5 sekund pobieral plik mojastrona.pl/powiadomienia.txt ktory przykladowo wyglada tak:
<http://december.com/html/4/element/span.html id="alerty" class="badge badge-danger badge-counter">15</http://december.com/html/4/element/span.html> .... <http://december.com/html/4/element/span.html id="pow" class="badge badge-success badge-counter">29</http://december.com/html/4/element/span.html>
window.addEventListener('load', function() { var xhr = null; getXmlHttpRequestObject = function() { if(!xhr) { // Create a new XMLHttpRequest object. xhr = new XMLHttpRequest(); } return xhr; }; updateLiveData = function() { var now = new Date(); // Date string is appended as a query with live data. // for not to use the cached version. var url = 'powiadomienia.txt'; xhr = getXmlHttpRequestObject(); xhr.onreadystatechange = evenHandler; // asynchronous requests xhr.open("GET", url, true); // Send the request over the network xhr.send(null); }; updateLiveData(); function evenHandler() { // Check response is ready or not if(xhr.readyState == 4 && xhr.status == 200) { dataDiv = document.getElementById('alerty'); // Set current data text dataDiv.innerHTML = xhr.responseText; // Update the live data every 1 sec setTimeout(updateLiveData, 1000); } } });
Twoje punkty znajduja sie teraz w responseText.
Uzyj wec np https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split by rozdzielic to po sredniku a potem po dwukropku i masz co chciales.
A nastepnym razem zapisuje dane jako JSON, bedzie prosciej
Split() zadziałał, dzięki!
W pliku wynikowym niepotrzebne są informacje "alerty" i "powiadomienia".
Wystarczy postać: 1;2
Do tego już też doszedłem
A jeszcze chaiłbym, że jeśli liczba alertów będzie większa od 0, to <title> strony zmieni się na "(1) Oryginalny tytuł" a gdy znowu spadnie do 0 to <title> wróci do "Oryginalny tytuł". Czy ktoś poratuje fragmentem kodu również i na to?
A znasz Google? Pewnie tak...
https://www.google.com/search?q=javascript+meta+tag+title
W google nie znalazłem gotowca. Zmodyfikowałem trochę swój kod:
function evenHandler() { // Check response is ready or not if(xhr.readyState == 4 && xhr.status == 200) { notifDiv = document.getElementById('pow'); creditDiv = document.getElementById('alerty'); // Set current data text const not = xhr.responseText.split(';'); notifDiv.innerHTML = not[0]; <------> if(not[0] > 0) { <------><------>document.title = "(" + not[0] + ") " + document.title; <------> } creditDiv.innerHTML = not[1]; // Update the live data every 1 sec setTimeout(updateLiveData, 1000); } } });
function evenHandler() { <------>var oldnot = 0; <------>var oldtitle = document.title; // Check response is ready or not if(xhr.readyState == 4 && xhr.status == 200) { notifDiv = document.getElementById('pow'); creditDiv = document.getElementById('alerty'); // Set current data text const not = xhr.responseText.split(';'); notifDiv.innerHTML = not[0]; <------> if(oldnot != not[0]) { <------><------>document.title = "(" + not[0] + ") " + oldtitle; <------><------>oldnot = not[0]; <------> } creditDiv.innerHTML = not[1]; // Update the live data every 1 sec setTimeout(updateLiveData, 1000); } } });
W jakim celu zapamiętywać poprzednią liczbę powiadomień?
Pobierasz wartość, doklejasz tytuł i wstawiasz.
Doklaejam ale robi mi sie z tego taki infinite loop. Co robie zle?
Liczba powiadomień>0 ? Liczba powiadomień + tytuł : Tytuł
Ten kod działa tak samo: (1) (1) się nawarstwia. Na razie zastosowałem obejście w postaci metatagu zawierającego to samo co tytuł:
var title = document.querySelector('meta[name="description"]').content; //Notifications if(not[0] > 0) { document.title = "(" + not[0] + ") " + title; } else { document.title = title; }
http://www.php.net/header("Location /login.php");
<http://december.com/html/4/element/meta.html http-equiv="refresh" content="5; URL=https://www.mojastrona.pl/login.php" />
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)