Web Analytics Made Easy -
StatCounter Searching the CDATA for img url with javascript. - CodingForum

Announcement

Collapse
No announcement yet.

Searching the CDATA for img url with javascript.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Searching the CDATA for img url with javascript.

    I am relatively new to javascript, and completely new to xml files.

    I was accessing a page for an image url to switch out the url of one on another page (all on the same domain).

    With some help in another forum on this site, I ended up with a script to do so, but it's clashing with programming on the site (a purchased program - nothing I can change)

    So my next idea is to do the same, but to pull from the xml rss feed in an iframe. Below is what I have for that.. how would I do this for xml files, to get an image url from within an

    Code:
    <script type="text/javascript" language="JavaScript">
    function find_div_class() {
           var iframe = document.getElementById('IFrame1');
           var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
           var info = innerDoc.getElementsByClassName("list-journal-entry-wrapper")[0];
    
           var theURL = null;  // assume not found
    
           var spans = info.getElementsByTagName("span");
           for ( var s = 0; s < spans.length; ++s )
           {
               // have to search through spans for the class name:
               var span = spans[s];
               if ( span.className.indexOf("full-image-float-left") >= 0 
                   && span.className.indexOf("ssNonEditable") >= 0 )
               {
                   // we found the right one
                   // get FIRST image inside that span:
                   var image = span.getElementsByTagName("img")[0];
                   // and then the image link:
                   theURL = image.src;
                   break; // quit when we find first one
               }
            }
            if ( theURL == null ) 
            {
                alert("I can't find the darned thing!");
            } else {
                
               var img = document.getElementById('lead1');
            img.src = (theURL);            
            return false;
            }
    
    } 
    </script>
    Here's the section within the xml file that I'm trying to get to:

    <description><![CDATA[<span class="full-image-float-left ssNonEditable"><span><img src="http://www.website.com/storage/image...=1314647067762" alt="" /></span></span>test entry 1 text]]>
    </description>

  • #2
    Originally posted by turpentyne View Post
    So my next idea is to do the same, but to pull from the xml rss feed in an iframe. Below is what I have for that.. how would I do this for xml files, to get an image url from within an
    Here's an example:

    document_1.xhtml
    Code:
    <?xml version="1.0"?>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
    	<head>
    		<title>Demo Document</title>
    		<style>
    			* { margin: 0; border: 0 none transparent; }
    			html { padding: 2em; }
    			iframe { padding: 1em; background-color: hsl(0, 0%, 90%); }
    		</style>
    		<script>
    			var d = document;
    			d.initialize = function () {
    				var XHTML_ns = "http://www.w3.org/1999/xhtml";
    				var iframe_el = d.getElementsByTagNameNS(XHTML_ns, "iframe")[0];
    				var description_el = iframe_el.contentDocument.getElementsByTagNameNS(null, "description")[0];
    				var URL = description_el.textContent.match(/(http:\/\/.*?)"/)[1];
    				d.defaultView.alert(URL);
    			};
    		</script>
    	</head>
    	<body onload="d.initialize();">
    		<iframe src="document_2.xml"></iframe>
    	</body>
    </html>
    document_2.xml
    Code:
    <?xml version="1.0"?>
    <description><![CDATA[<img alt="…" width="…" height="…" src="http://www.example.com/image.png"></img>]]></description>

    Comment

    Working...
    X