ExtJS's XmlReader field mapping - javascript

I can't manage to get this Ext.data.XmlReader's CDATA field mapping to work.
<script>
var store = new Ext.data.Store({
url: '../data/data.xml',
// specify a XmlReader
reader: new Ext.data.XmlReader({
record: 'entry',
fields:[
{ name: 'field1', type: 'date', mapping:'field1'},
{ name: 'field2', type: 'string', mapping:'field2'}
]
}),
listeners:{load:function(store,recs)
{ //alert row1.field1 and row1.field2
var s = 'field1 = '+recs[0].get('field1') + '\nfield2 = '+recs[0].get('field2');
alert(s);
}
}
});
store.load();
</script>
And here's the XML contents in data.xml :
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<field1>01/01/2006</field1>
<field2>
<![CDATA[
<Comment>
Test
</Comment>
]]>
</field2>
</entry>
</feed>
When store finished loading . The alert (from the listener) shows some thing like this:
field1 = Sun Jan 01 2006 00:00:00 GMT+0700 (ICT)
field2 =
But I expected to see this :
field1 = Sun Jan 01 2006 00:00:00 GMT+0700 (ICT)
field2 = <Comment>
Test
</Comment>
These issue only happen in chrome and safari.it works with IE6.
How do I get the field2 node value (preferably, the solution works across major browsers),
any suggestion ?
Thanks in advance.
Owat

The <![CDATA[ start tag must start immediately after the XML tag with no space in between and the ]]> end tag must be followed immediately by the XML tag close, like this:
<field2><![CDATA[
<Comment>
Test
</Comment>
]]></field2>

Related

Delete cookie for Mailchimp Popup

I have a wordpress website where you click on a div, and a mailchimp popup form pops up. One issue is, mailchimp stores a cookie to make sure the popup is only done once. But since I'm now doing it on a click, I want to get rid of that cookie.
This is my code:
var mailchimpConfig = {
baseUrl: 'mc.us17.list-manage.com',
uuid: '1356322e2......rest of my code',
lid: '36776d...rest of my code'
};
// No edits below this line are required
var chimpPopupLoader = document.createElement("script");
chimpPopupLoader.src = '//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js';
chimpPopupLoader.setAttribute('data-dojo-config', 'usePlainJson: true, isDebug: false');
var chimpPopup = document.createElement("script");
chimpPopup.appendChild(document.createTextNode('require(["mojo/signup-forms/Loader"], function (L) { L.start({"baseUrl": "' + mailchimpConfig.baseUrl + '", "uuid": "' + mailchimpConfig.uuid + '", "lid": "' + mailchimpConfig.lid + '"})});'));
jQuery(function ($) {
document.body.appendChild(chimpPopupLoader);
jQuery(".coming-soon").on("click", function () {
alert("Hello");
document.body.appendChild(chimpPopup);
document.cookie = 'MCEvilPopupClosed=;path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC;';
});
});
So after searching the internet, I found that everyone used this code to remove the cookie.
document.cookie = 'MCEvilPopupClosed=;path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC;';
This issue is, it's not working for me. I've done all testing in incognito and can only get the pop up to work once.
The cookie's name is MCPopupClosed rather than MCEvilPopupClosed. I'm not sure why, but MailChimp must have changed it at some point.
There's also another cookie called MCPopupSubscribed, which is set if the user subscribes instead of closing the popup with the 'X' button. If you want the popup to display even if the user has subscribed, you'll want to clear that cookie as well.
Clearing those two cookies will only make it work once after the page loads, though. I came across this code while looking into the issue, and it works fine if you put the require in the click function instead of a script tag. Doing this also prevents having to remove the script tags generated by the appendChild function every time the element is clicked.
So your updated code looks like this:
var mailchimpConfig = {
baseUrl: 'mc.us17.list-manage.com',
uuid: '1356322e2......rest of my code',
lid: '36776d...rest of my code'
};
// No edits below this line are required
var chimpPopupLoader = document.createElement("script");
chimpPopupLoader.src = '//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js';
chimpPopupLoader.setAttribute('data-dojo-config', 'usePlainJson: true, isDebug: false');
jQuery(function ($) {
document.body.appendChild(chimpPopupLoader);
jQuery(".coming-soon").on("click", function () {
require(["mojo/signup-forms/Loader"], function (L) { L.start({"baseUrl": mailchimpConfig.baseUrl, "uuid": mailchimpConfig.uuid, "lid": mailchimpConfig.lid})});
document.cookie = 'MCPopupClosed=;path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC;';
document.cookie = 'MCPopupSubscribed=;path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC;';
});
});
you missed a cookie:
document.cookie = "MCEvilPopupClosed=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
I've just corrected the exact same problem.
Hope this helps,
P.

Javascript copy to clipboard including html formatted text

I'm relatively new to js so would like some help,
I've got a form which is generated in php. I want the user to be able to click a button and copy the flight results to the clipboard,
I have the following javascript function:
<script>
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val($(element).html().replace(brRegex, "\r\n")).select();
document.execCommand("copy");
$temp.remove();
}
</script>
However when you paste the result i get the following with the formatting tags visible:
<b>Mon 09 Oct - DY 7015 </b>
Depart: London Gatwick Airport, (LGW) at 17:05
Arrive: John F Kennedy Airport, New York (JFK) at 20:05
I want the result input to be
Mon 09 Oct - DY 7015
Depart: London Gatwick Airport, (LGW) at 17:05
Arrive: John F Kennedy Airport, New York (JFK) at 20:05
or if this is not possible easily, then at the very least display without formatting but also without the tags
Mon 09 Oct - DY 7015
Depart: London Gatwick Airport, (LGW) at 17:05
Arrive: John F Kennedy Airport, New York (JFK) at 20:05
Any ideas?
You can try this regex to remove your HTML tags: /<\/?[a-zA-Z]+\/?>/g
So, this should work :
$(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '')
Hope this helps!

Parsing an RSS feed containing CDATA using jQuery

I am trying to parse an RSS feed into an array but the feed is adding CDATA tags and combining certain elements.
My code below parses through the rss feed (url) and adds certain elements to an array. However when I look at the feed itself, it is combining multiple key elements in CDATA tags.
How do I parse through the CDATA tags to get usable xml fields?
Code
buildXMLDoc = function (url) {
var list =[];
$(listXML).find('item').each(function (){
var el = $(this);
console.log(el.find("title").text());
console.log(el.find("pubDate").text());
console.log(el.find("description").text());
list.push({title: el.find("title").text(), description: el.find("description").text(), modified: el.find("pubDate").text()});
});
return list;
};
XML
<?xml version="1.0" encoding="UTF-8"?>
<!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 8/29/2017 10:23:18 AM -->
<?xml-stylesheet type="text/xsl" href="/_layouts/RssXslt.aspx?List=43aaf08e-0153-4f1d-9b46-e66bba563fde" version="1.0"?>
<rss version="2.0">
<channel>
<title>Webdocs: Test</title>
<description>RSS feed for the Test list.</description>
<lastBuildDate>Tue, 29 Aug 2017 14:23:18 GMT</lastBuildDate>
<generator>Microsoft SharePoint Foundation RSS Generator</generator>
<ttl>60</ttl>
<language>en-US</language>
<item>
<title>Alternative Methods for Determining LCRs</title>
<description><![CDATA[<div><b>Short Title:</b> Determining LCRs</div>
<div><b>Description:</b> <div class="ExternalClass6280076BC79848078688B86006BA554F"><p>​<span style="font-size:11.5pt;font-family:"calibri", "sans-serif"">This project is a carryover from the 2017 effort to identify an alternative method for calculating the Locational Minimum Installed Capacity Requirements (LCRs). </span></p></div></div>
<div><b>Governance Process Status:</b> Progress</div>
<div><b>Topic State:</b> Open/Current</div>
<div><b>Updated Placeholder:</b> updated</div>
]]></description>
<pubDate>Wed, 12 Jul 2017 13:41:06 GMT</pubDate>
</item>
The highlighted items are suppose to be separate elements.
In order to get the CDATA part details I may suggest to use jquery.contents() and so getting the relative sub sections by positon. This may give you wrong results if the positions change but it's a possibility.
var listXML = '<?xml version="1.0" encoding="UTF-8"?>\
<!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 8/29/2017 10:23:18 AM -->\
<?xml-stylesheet type="text/xsl" href="/_layouts/RssXslt.aspx?List=43aaf08e-0153-4f1d-9b46-e66bba563fde" version="1.0"?>\
<rss version="2.0">\
<channel>\
<title>Webdocs: Test</title>\
<description>RSS feed for the Test list.</description>\
<lastBuildDate>Tue, 29 Aug 2017 14:23:18 GMT</lastBuildDate>\
<generator>Microsoft SharePoint Foundation RSS Generator</generator>\
<ttl>60</ttl>\
<language>en-US</language>\
<item>\
<title>Alternative Methods for Determining LCRs</title>\
<description><![CDATA[<div><b>Short Title:</b> Determining LCRs</div>\
<div><b>Description:</b> <div class="ExternalClass6280076BC79848078688B86006BA554F"><p>​<span style="font-size:11.5pt;font-family:"calibri", "sans-serif"">This project is a carryover from the 2017 effort to identify an alternative method for calculating the Locational Minimum Installed Capacity Requirements (LCRs). </span></p></div></div>\
<div><b>Governance Process Status:</b> Progress</div>\
<div><b>Topic State:</b> Open/Current</div>\
<div><b>Updated Placeholder:</b> updated</div>\
]]></description>\
<pubDate>Wed, 12 Jul 2017 13:41:06 GMT</pubDate>\
</item>';
var list =[];
$(listXML).find('item').each(function (){
var el = $(this);
var cdat = $(listXML).find('item description').contents();
console.log(cdat.eq(1).text() + cdat.eq(2).text());
console.log(cdat.eq(5).contents().eq(0).text() + cdat.eq(5).contents().eq(1).text());
console.log(cdat.eq(6).contents().eq(0).text() + cdat.eq(6).contents().eq(1).text());
list.push({title: cdat.eq(2).text(), description: cdat.eq(5).contents().eq(1).text(), modified: cdat.eq(6).contents().eq(1).text()});
});
console.log('list: ' + JSON.stringify(list));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
A different approach is to get the description element, replace the inner CDATA and convert the result to a jQuery object. On this object you can use find in order to select sub elements.
var listXML = '<?xml version="1.0" encoding="UTF-8"?>\
<!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 8/29/2017 10:23:18 AM -->\
<?xml-stylesheet type="text/xsl" href="/_layouts/RssXslt.aspx?List=43aaf08e-0153-4f1d-9b46-e66bba563fde" version="1.0"?>\
<rss version="2.0">\
<channel>\
<title>Webdocs: Test</title>\
<description>RSS feed for the Test list.</description>\
<lastBuildDate>Tue, 29 Aug 2017 14:23:18 GMT</lastBuildDate>\
<generator>Microsoft SharePoint Foundation RSS Generator</generator>\
<ttl>60</ttl>\
<language>en-US</language>\
<item>\
<title>Alternative Methods for Determining LCRs</title>\
<description><![CDATA[<div><b>Short Title:</b> Determining LCRs</div>\
<div><b>Description:</b> <div class="ExternalClass6280076BC79848078688B86006BA554F"><p>​<span style="font-size:11.5pt;font-family:"calibri", "sans-serif"">This project is a carryover from the 2017 effort to identify an alternative method for calculating the Locational Minimum Installed Capacity Requirements (LCRs). </span></p></div></div>\
<div><b>Governance Process Status:</b> Progress</div>\
<div><b>Topic State:</b> Open/Current</div>\
<div><b>Updated Placeholder:</b> updated</div>\
]]></description>\
<pubDate>Wed, 12 Jul 2017 13:41:06 GMT</pubDate>\
</item>';
var list =[];
$(listXML).find('item').each(function (){
var el = $(this);
var cdat = $(listXML).find('item description').contents();
var html = $($(listXML).find('item description')[0].innerHTML.replace('<!--[CDATA[', '')).html();
console.log(html);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

how to find the latest element from the group of nodes where the attribute of the element represents the date and time. in xml

<data>
<manufacture date="2013-06-05 T 19:40:50. 88463 7 Z">
<title>java_package</title>
<author>tom</author>
<year>2013</year>
<price>29.99</price>
</manufacture>
<manufacture date="2015-06-05T19:40:50.884637Z">
<title>java_package_2</title>
<author>tom</author>
<year>2015</year>
<price>39.95</price>
</manufacture>
<manufacture date="2014-06-05T19:40:50.884637Z">
<title>java_package_3</title>
<author>tom</author>
<year>2003</year>
<price>39.95</price>
</manufacture>
</data>
DATA is the root element, Manufacture is the element content.
here i need to get the latest manufacture, based on date i.e
2015-06-05 T 19 : 40 : 50 . 88463 7 Z
which is an attribute of the manufacture element.
i am doing in asp.net and c#
here is my code that i tried
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml("bc.xml");
XmlNode root = xdoc.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("//manufacture");
foreach (XmlNode node1 in nodeList)
{
Label1.Text += node.Attributes["DATE"].Value.ToString() + "\n <br>";
}
Your datetime formatting in the file is wrong and inconsistent....
It should be like this : "2013-06-05 T19:40:50.884637Z" otherwise any and every code will fail...
Here is the solution...
var mf = doc.Root.Elements("manufacture")
.OrderByDescending (m => DateTime.ParseExact(m.Attribute("date").Value, "yyyy-MM-dd THH:mm:ss.ffffffZ", null))
.First();

Cookie (document.cookie)

Iep, i have a problem, its the first time that im working with cookies and i was trying to save them.
The question is that only the 2 first values are saved, in this case "nombre" and "tuValor". If i do "alert(document.cookie)" the other values dont apear.
<script type="text/javascript">
function guardar() {
Nombre = "Empire";
tuValor = "F"+food;
tuValor2 = "w"+wood;
caduca = "31 Dec 2020 23:59:59 GMT";
document.cookie = Nombre+"="+tuValor+tuValor2+"expire= "+caduca ;
}
</script>
You forgot the semicolon before expire.
It goes like this:
document.cookie="my_cookie=empireWh4t3v3r;expires=Thu, 23 Apr 2020 15:37:15 GMT;"

Categories