javascript - Store another page H1 content in var and check if there's a word -
i have index page , page called 'p1.html'. need store h1 content p1.html, onto var on index page, check later, if h1 in var contains specific word
something like:
var h1content = $.get('p1.html', function(html){ $.text($('h1',html).text()); }); // other code blablabla // other code blablabla // other code blablabla if (h1content.indexof("stock") != -1) { console.log("h1 has stock"); } else if (h1content.indexof("turismo") != -1) { console.log("h1 has turismo"); }
the p1.html h1 here:
<h1 id="title">camp. bras. stock car - 2ยช bateria</h1>
what doing wrong?
the idea can use above concept, within code:
var timerload, timerchange; var maxnum, rafraichir, changement, classementreduit, classementreduitxpremier; var urlrefresh, urlchange; rafraichir = 3000; changement = 15000; maxnum = 1; classementreduit = 0; classementreduitxpremier = 10; // var h1 p1 stored goes here <<<<< function load(url, target) { var xhr; var fct; if (urlchange) url = urlrefresh; else urlrefresh = url; urlchange = 0; if (timerload) cleartimeout(timerload); try { xhr = new activexobject("msxml2.xmlhttp") } catch (e) { try { xhr = new activexobject("microsoft.xmlhttp") } catch (e2) { try { xhr = new xmlhttprequest } catch (e3) { xhr = false } } } xhr.onreadystatechange = function() { if (xhr.readystate == 4 && xhr.status == 200) if (classementreduit == 0) document.getelementbyid(target).innerhtml = xhr.responsetext; else document.getelementbyid(target).innerhtml = extraireclassementreduit(xhr.responsetext) //console.log("18"); // if/else conditions check if h1 have specific word goes here <<< };
you should doing:
var h1content; $.get('p1.html', function(html){ h1content = $('h1',html).text(); }); function checkh1() { if (h1content) { if (h1content.indexof("stock") != -1) { console.log("h1 has stock"); } else if (h1content.indexof("turismo") != -1) { console.log("h1 has turismo"); } } else { settimeout(checkh1, 1000); //in case h1content empty because ajax call didn't retrieve yet, waits 1 second , fires function again. } } checkh1(); //you can call wherever want, may attaching event.
don't forget $.get()
asynchronous call so, process won't block until gets response, continue working , run lines below $.get()
call , variable h1content undefined
.
Comments
Post a Comment