0

Good, I come again, ask for your help, I have a code that provides me with data and it has a DIV.

<div id="teste1"></div>

What I wanted was to refresh the DIV and there is no whole page, I've been to see jQuery and AJAX code but they do connect to a PHP or HTML page and in my case the code is inserted in the homepage.

<script>
//http://code.google.com/p/miniajax/
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
 
ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};
 
function process(xml) {
  document.getElementById('teste1').innerHTML=xml;
  var title = document.getElementById('teste1').getElementsByTagName('song')[0].title;
  var name = document.getElementById('teste1').getElementsByTagName('Artist')[0].getAttribute('name');
  document.getElementById('teste1').innerHTML=' '+name+' - ' +title;
}
function checkXml() {
  ajax.get('/NowOnAir.xml',process)
}
window.onload=function() {
  checkXml();
  tId=setInterval('checkXml()',speed)  
}

</script>

Someone can help me?

Thank you so much!

André Santos
  • 55
  • 1
  • 7
  • 1
    Where is the data that you want to change to stored? – Matt Feb 21 '16 at 00:10
  • I dont understand what the problem is. can you explain better? – CodeGodie Feb 21 '16 at 00:12
  • @Matt I added the code I want to do the refresh – André Santos Feb 21 '16 at 00:14
  • Have you considered using an Ajax call and refresh the data in the div with the response of the call? – Bigalow Feb 21 '16 at 00:15
  • @CodeGodie I put the code, which I intend to do is refresh the div that provides code information – André Santos Feb 21 '16 at 00:15
  • @Bigalow I've tested several codes, some do not work, others this div placed on other pages, which is not the case, because either DIV want the code estam both on the same page – André Santos Feb 21 '16 at 00:17
  • It seems that you are just appending the contents of the xml file to the div, and not parsing it. – IeuanG Feb 21 '16 at 00:18
  • I dont see jQuery here – CodeGodie Feb 21 '16 at 00:20
  • @leuanG This javascript retrieve information from an XML document, the code to me this working well what is missing is to get to refresh the DIV, so I came to ask your help – André Santos Feb 21 '16 at 00:20
  • @CodeGodie https://jsfiddle.net/vyq1daeu/ I am trying here – André Santos Feb 21 '16 at 00:24
  • Im sorry Andre, this code is very messy. Check out this page: http://stackoverflow.com/help/on-topic _"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."_ – CodeGodie Feb 21 '16 at 00:27
  • @CodeGodie I can retire my question? – André Santos Feb 21 '16 at 00:31
  • @DenizEng The XML file is this http://radiojoven.6te.net/NowOnAir.xml he calls because it can withdraw from there information – André Santos Feb 21 '16 at 00:34
  • 2
    Can't make any sense out of what the specific problem is here – charlietfl Feb 21 '16 at 00:39
  • Is your app also on the same domain(radiojoven.6te.net) Andre Santos? Otherwise you'll get below error(on JSFiddle as well) and will need to use CORS `XMLHttpRequest cannot load http://radiojoven.6te.net/NowOnAir.xml. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.` – DenizEng Feb 21 '16 at 00:42
  • @DenizEng I for example have this page http://radiojoven.6te.net/example.php where is the code I provide more DIV, and what I wanted was to refresh only the DIV and not to the page, I've had to search codes and more codes and can not find one I do – André Santos Feb 21 '16 at 00:45
  • @charlietfl I am very grateful for the support the resolution, today I spent the day researching and testing codes and none gave – André Santos Feb 21 '16 at 00:47
  • `tId=setInterval('checkXml()',speed)` here the speed variable is undefined Andre, you need to set it. – DenizEng Feb 21 '16 at 00:48
  • @DenizEng tid = setInterval ('check XML ()', 20000) something? – André Santos Feb 21 '16 at 00:52
  • If `speed` is undefined it would throw error...are you seeing errors in browser console? – charlietfl Feb 21 '16 at 00:53
  • @AndréSantos yes. But that's not the only issue. You're receiving the XML response successfully, but then, you need to parse it instead of this line and then use the values. `document.getElementById('contentfile').innerHTML=xml;` Here is a sample for parsing XML in vanilla JS: http://stackoverflow.com/questions/7949752/cross-browser-javascript-xml-parsing – DenizEng Feb 21 '16 at 00:54
  • @DenizEng Thank you, I could already do the refresh as intended, thank you again! – André Santos Feb 21 '16 at 01:01
  • @charlietfl Thank you, I could already do the refresh as intended, thank you again! – André Santos Feb 21 '16 at 01:02
  • Np. Is your problem resolved? – DenizEng Feb 21 '16 at 01:03
  • If you use the XML parser in the link that I provided above, the below will give you the song name, you can experiment to find other values: `parseXml(xml).getElementsByTagName('Song')[0].attributes[0].value` – DenizEng Feb 21 '16 at 01:11
  • Also note that the tagname search is case sensitive, so you should have uppercase Song as per the xml file unlike your code in the question. – DenizEng Feb 21 '16 at 01:13
  • @DenizEng DIV already does the refresh as I wanted tid = setInterval ('XML check ()', 20000) Using your way also can access the title and song name – André Santos Feb 21 '16 at 01:19
  • @DenizEng I so far had no problems with the code in question, provides exactly what is asked – André Santos Feb 21 '16 at 01:22
  • Great, I've summarized my comments into an answer, please accept if it helped. – DenizEng Feb 21 '16 at 01:43
  • @DenizEng Done and thank you for the help and all. – André Santos Feb 21 '16 at 01:47

1 Answers1

1

Firstly, your speed variable in window.onload handler is undefined, needs to be set to a number value. Secondly, you need to parse the XML response before traversing the elements. Link with sample: Cross-Browser Javascript XML Parsing

You can use the xml parsing function above and the below code to get the song name and use the same approach to find the values of other elements.

parseXml(xml).getElementsByTagName('Song')[0].attributes[0].value

Community
  • 1
  • 1
DenizEng
  • 400
  • 4
  • 14