Brett
Brett

Reputation: 107

.parseHTML() alternative for jquery 1.7?

I have an ajax function to load image from an RSS feed but getting a .parseHTML is not a function error since I'm still using 1.7.2. Unfortunately, upgrading to 1.8 is not an option...

$.ajax({
    type: "GET",
    url: "url.com/feed",
    dataType: "xml",
    success: function(xml) {
        var limit = 1;
        $(xml).find('item').each(function(index) {
            if (index < limit) {
                var title = $(this).find('title').text();
                var url = $(this).find('link').text();
                var pubDate = $(this).find('pubDate').text();
                var desc = $(this).find('description').text();
                desc = $.parseHTML(desc);
                var imgURL = desc[0].firstChild.srcset.split(',').pop().trim().split(' ')[0];
                var alt = desc[0].firstChild.alt;
                // Do something
                return;
            }
        }); //end each
    }
}); 

I'm using .praseHTML() to target the last srcset attribute of the image to get its URL. Here is what my RSS looks like

<item>
        <title>Geoffrey Martinez Brews Up a Good Cup of Java for Compton</title>
        <link>https://csudhnews.staging.wpengine.com/geoffrey-martinez/</link>
        <pubDate>Wed, 08 Aug 2018 16:09:29 +0000</pubDate>
        <dc:creator xmlns:dc='http://purl.org/dc/elements/1.1/'><![CDATA[Paul Browning]]></dc:creator>
                <category><![CDATA[Features]]></category>
        <category><![CDATA[Alumni]]></category>
        <category><![CDATA[Business]]></category>
        <category><![CDATA[Entrepreneurship]]></category>
        <category><![CDATA[Latin American Studies]]></category>

        <guid isPermaLink='false'>https://csudhnews.staging.wpengine.com/?p=32594</guid>
        <description><![CDATA[<div><img width='300' height='168' src='https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-300x168.jpg' class='attachment-medium size-medium wp-post-image' alt='Alumnus Geoffrey Martinez of Patria Coffee' style='margin-bottom: 15px;' srcset='https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-300x168.jpg 300w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-768x430.jpg 768w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-1024x573.jpg 1024w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-630x350.jpg 630w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-750x420.jpg 750w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1-200x111.jpg 200w, https://csudhnews.staging.wpengine.com/wp-content/uploads/2018/08/Geoffrey-Martinez0006_FEATURE-1.jpg 1500w' sizes='(max-width: 300px) 100vw, 300px' /></div>It wasn’t the billowing smoke filling his apartment courtyard from a popcorn machine with a convention oven mounted on top that initially piqued Geoffrey Martinez’s curiosity, it was the smell of freshly roasting coffee beans that drew him in. “I had never seen coffee roasting before so it definitely caught my attention,” said the California [&#8230;]]]></description>
</item>

Upvotes: 2

Views: 1924

Answers (3)

jeetaz
jeetaz

Reputation: 731

If you're having XML document already in response via responseXML, you can query description directly, but if you're having XML string, you can use DOMParser to parse XML (or HTML) source code from a string into a DOM Document.

var xmlString = "YOUR XML STRING";
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlString, "text/xml");
console.log(xmlDoc.querySelector("description"));

Upvotes: 1

mpm
mpm

Reputation: 20155

You really don't need jQuery here. The X in Ajax stands for XML at first place.

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML

use responseXML property to obtain a document then you can use classic DOM methods like element.querySelector on it.

Upvotes: 3

Jack Bashford
Jack Bashford

Reputation: 44125

The parseHTML() method was only added in jQuery 1.8, but there is a GitHub issue here that should help. Essentially what you should do is extract the file from jQuery and use it with helpers, but this isn't that reliable. Why can't you use jQuery 1.8+?

Upvotes: 0

Related Questions