marklarah Posted February 13, 2009 Share Posted February 13, 2009 I want to get the value of an element (a span in this case), and display it on my page. Told to do this in javascript, all I have come up with is var target = ''; target.setAttribute( "src", "urlhere" ); target = document.getElementsByTagName( "span" )[3].value; It obviously doesn't work. The element doesn't actually have an ID, just it's class is unique. So how would I call the text from just the element on the page? I don't know where to start. Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/ Share on other sites More sharing options...
sdi126 Posted February 13, 2009 Share Posted February 13, 2009 finding elements by className is not natively supported in all major browsers :-X but I have used this javascript function before for what you are needing to do. function getElementsByClassName(className, tag, elm){ var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)"); var tag = tag || "*"; var elm = elm || document; var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag); var returnElements = []; var current; var length = elements.length; for(var i=0; i<length; i++){ current = elements[i]; if(testClass.test(current.className)){ returnElements.push(current); } } return returnElements; } ----->http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/ Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-761033 Share on other sites More sharing options...
marklarah Posted February 14, 2009 Author Share Posted February 14, 2009 Hmmm....yes, but I also need to be able to do this function on an external page Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762096 Share on other sites More sharing options...
marklarah Posted February 14, 2009 Author Share Posted February 14, 2009 Also, I've been using this that I found on the internet var allHTMLTags = new Array(); function getElementByClass(theClass) { //Create Array of All HTML Tags var allHTMLTags=document.getElementsByTagName(”*”); //Loop through all tags using a for loop for (i=0; i<allHTMLTags.length; i++) { //Get all tags with the specified class name. if (allHTMLTags[i].className==theClass) { //Place any code you want to apply to all //pages with the class specified. //In this example is to “display:none;” them //Making them all dissapear on the page. allHTMLTags[i].style.display=’none’; } } } Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762101 Share on other sites More sharing options...
marklarah Posted February 14, 2009 Author Share Posted February 14, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762370 Share on other sites More sharing options...
marklarah Posted February 14, 2009 Author Share Posted February 14, 2009 yah nobody cares about this board. Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762415 Share on other sites More sharing options...
marklarah Posted February 15, 2009 Author Share Posted February 15, 2009 bumpity Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762447 Share on other sites More sharing options...
marklarah Posted February 15, 2009 Author Share Posted February 15, 2009 I'll give y'all a cookie? Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762466 Share on other sites More sharing options...
RichardRotterdam Posted February 15, 2009 Share Posted February 15, 2009 Hmmm....yes, but I also need to be able to do this function on an external page Depends if that external page is on a different domain javascript can't be cross domain. Even if it is on the same server PHP is probably better to use. You can use cURL or just parse the page to a string. Then you could do a regex to fetch the element you want. Or use DOM Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762723 Share on other sites More sharing options...
marklarah Posted February 15, 2009 Author Share Posted February 15, 2009 uh... Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-762868 Share on other sites More sharing options...
haku Posted February 16, 2009 Share Posted February 16, 2009 Is the page on a different domain? Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-763197 Share on other sites More sharing options...
marklarah Posted February 16, 2009 Author Share Posted February 16, 2009 Yes Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-763384 Share on other sites More sharing options...
Adam Posted February 16, 2009 Share Posted February 16, 2009 ... Then reading the contents of that page is beyond JavaScript. You'll need to look into using a server-side programming language like PHP; more specifically curl or one of the file functions. Adam Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-763388 Share on other sites More sharing options...
marklarah Posted February 17, 2009 Author Share Posted February 17, 2009 I know I can read external pages in PHP, but I need to be able to pick out certain elements. Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-764261 Share on other sites More sharing options...
Adam Posted February 17, 2009 Share Posted February 17, 2009 Regular expressions... Adam Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-764265 Share on other sites More sharing options...
RichardRotterdam Posted February 17, 2009 Share Posted February 17, 2009 Or parse it using the DOMdocument class domdocument Quote Link to comment https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/#findComment-764339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.