IE11 not recognizing .loadXML() - javascript

Working on some xml data and my function works in all browsers except for IE10 and up.
Is there something else I can use instead of .loadXML(data)
Here is part of the transform function. it breaks at x.loadXML(data)
$.transform = function(o) {
var createXmlObj = function(data) {
if($.browser.msie) {
var x = $("<xml>")[0];
x.loadXML(data);
return x;
} else {
var parser = new DOMParser();
return parser.parseFromString(data,"text/xml");
}
};

add your page to Internet Explorer's Compatibility View Settings. You won't believe the types of issues that this fixes.

Related

Create Xml document based on the browser using Javascript

I am trying to get an older application to work in Chrome and Edge. It currently works only in Compatibility Mode. I have narrowed it down to where it gets the browser type and then creates the xml document based on the browser type.
// Identify and return the browser type
function getBrowserType()
{
try
{
if (typeof ActiveXObject != 'undefined')
{
//Microsoft Internet Explorer
return 'MSIE';
}
else if (typeof document != 'undefined'
&& document.implementation
&& document.implementation.createDocument
&& typeof DOMParser != 'undefined')
{
//Other browsers
return 'OTH';
}
}
catch(ex)
{
alert('Unable to find browser type.\n' + ex.message);
}
}
//create the xml DOM by browser detection
function createDoc(browserType)
{
var xmlDOM = null;
if (browserType == 'MSIE')
{
try
{
var names = [ 'Msxml2.DOMDocument.6.0',
'Msxml2.DOMDocument.3.0',
'MSXML2.DOMDocument',
'MSXML.DOMDocument',
'Microsoft.XMLDOM' ];
for (var key in names)
{
try
{
xmlDOM = new ActiveXObject(names[key]);
}
catch(ex)
{}
}
}
catch(ex)
{
alert('Unable to create XML Document.\n' + ex.message);
}
}
else if (browserType == 'OTH')
{
try
{
xmlDOM = document.implementation.createDocument("", "", null);
}
catch(ex)
{
alert('Unable to create XML Document.\n' + ex.message);
}
}
return xmlDOM;
}
So the object that is created is incorrect and it will not create the xml document. If the object created is the ActiveXObject, in Compatibility Mode, it has no problem. So this is based on the browser. So I basically need a way for this to work in all browsers. I think there may be a better way to do this as this is very old 2009 code.
I have tested your code on my side, it seems that I can't detect the IE browser (I'm using IE 11).
I suggest you could try to detect the browser using the window.navigator.userAgent property. code as below:
if (window.navigator.userAgent.toLowerCase().indexOf("trident") > -1) {
//Microsoft Internet Explorer
return "IE";
}
Because, the Browser Agent strings as below:
[Note] The navigator data can be changed by the browser owner, if the userAgent was not modified, user could use the above method to detect the browser using JavaScript.
Then, you could refer to this article to create XML DOM.

copy to clipboard is not working in firefox 25

how can I implement "Clipboard" functionality in Firefox V25 . Even though I've changed in "about:config" the "dom.event.clipboardevents.enabled" to true as well as "clipboard.autocopy" to true , then also it's not working . Kindly give me a solution for this problem.
I used this piece of code for clipboard operation:
function copyToClipboardCrossbrowser(s) {
s = document.getElementById(s).value;
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text", s);
}
else{
// You have to sign the code to enable this or allow the action in about:config by changing
//user_pref("signed.applets.codebase_principal_support", true);
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var clip = Components.classes["#mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;
// create a transferable
var trans = Components.classes["#mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans) return;
// specify the data we wish to handle. Plaintext in this case.
trans.addDataFlavor('text/unicode');
// To get the data from the transferable we need two new objects
var str = new Object();
var len = new Object();
var str = Components.classes["#mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data= s;
trans.setTransferData("text/unicode",str, str.data.length * 2);
var clipid=Components.interfaces.nsIClipboard;
if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
}
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect')
do not work in current version of firefox because of security considarations - seaching for a solution myself!

jQuery xmlParse not working in IE

I am building a photo slider in JavaScript and jQuery. It works perfectly in chrome, but not in IE6 where I know most of my clients would view it.
I have this function:
function getFacebookPhotos(photoCount, pageId) {
var picsUrl = "http://api.facebook.com/method/fql.query?query=SELECT%20src_big,%20src_big_height,%20src_big_width%20FROM%20photo%20WHERE%20pid%20IN%20(SELECT%20pid%20FROM%20photo_tag%20WHERE%20subject='243117879034102')%20OR%20pid%20IN%20(SELECT%20pid%20FROM%20photo%20WHERE%20aid%20IN%20(SELECT%20aid%20FROM%20album%20WHERE%20owner='" + pageId + "'%20AND%20type!='profile'))";
var responseText = $.ajax({
url: picsUrl,
async: false,
dataType: "xml",
success: function(text) {
responseText = text;
}
}).responseText;
var xmlDoc = $.parseXML(responseText);
var $xml = $(xmlDoc);
var $photos = $xml.find("photo");
var resultantPhotos = [];
for (var i = 0; i < photoCount; i++)
{
var $element = $($photos[i]);
var $src_big = $element.find("src_big");
var $text = $src_big.text();
resultantPhotos.push($text);
}
return resultantPhotos;
}
It fetches the XML response from a facebook query, parses it, and returns an array of photo urls from a facebook page. In Chrome, this works, but in Internet Explorer 6, the returned photo array is null. In both browsers the code executes without error.
I was using this JavaScript to parse the XML:
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open("GET", picsUrl, false); // Throws permission error in IE
xmlHttp.send(null);
var photos;
if (window.DOMParser)
{
var parser = new DOMParser();
xml = parser.parseFromString(responseText, "text/xml");
}
else // Internet Explorer
{
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(responseText);
}
photos = xml.getElementsByTagName("photo");
But that gave me errors in IE while still working in Chrome so I switched to jQuery.
Do you know what's wrong with it?
This may or may not be accurate, but I think the solution is to not use javascript at all, but to use PHP instead.
My errors were caused by javascript's lack of permission to access remote files. When I tried retrieving the XML output first with this PHP code:
<?php file_put_contents("query.xml", file_get_contents($facebookPicsUrl)); ?>
My javascript code
xml.open("GET","query.xml",false);
worked perfectly, but it made me realize that I should be using PHP, not only because downloading an xml file to my server is clunky, but because PHP can do everything that my JS is doing with simplexml_load_file. Everywhere I went looking for "how to get IE to open remote XML," they flat out say "you can't." Regardless of whether or not it's possible, it will be much easier to do this in PHP.

XML to JSON works in firefox but produces TypeError in chrome

Im porting a browser extension from FF to chrome. I have this XMLHttpRequest, which works fine:
var xhrdata = new XMLHttpRequest(),
xhrdata.onreadystatechange = function () {
if (xhrdata.readyState === 4) {
if (xhrdata.status === 200) {
getJXONTree(xhrdata.responseXML);
}
}
};
xhrdata.open("GET", "mydomain.com/my.xml", true);
xhrdata.responseType = "document";
xhrdata.send();
This send the .responseXML over to this function (shortened)
function getJXONTree(oXMLParent) {
var vResult = true, nLength = 0, sCollectedTxt = '';
if (oXMLParent.hasAttributes()) {
vResult = {};
[...]
This works absolutely fine in firefox, but in chrome, polling the exact same XML with the exact same code, I get this error:
TypeError: Object #<Document> has no method 'hasAttributes'
What am I missing here?
Firefox is more lenient when it comes to this, but it has to be:
xhr.responseXML.documentElement
since documents dont have any attributes. thanks #robW

Javascript Copy Clipboard function which works perfectly on all latest browsers

I have got below cocpy clipboard function which works perfectly on latest Internet Explorer as well as Latest Firefox.
function copyToClipboardCrossbrowser(s)
{
//s = document.getElementById(s).value;
if( window.clipboardData && clipboardData.setData )
{
clipboardData.setData("Text", s);
}
else
{
// You have to sign the code to enable this or allow the action in about:config by changing
//user_pref("signed.applets.codebase_principal_support", true);
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var clip = Components.classes["#mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;
// create a transferable
var trans = Components.classes["#mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans) return;
// specify the data we wish to handle. Plaintext in this case.
trans.addDataFlavor('text/unicode');
// To get the data from the transferable we need two new objects
var str = new Object();
var len = new Object();
var str = Components.classes["#mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data= s;
trans.setTransferData("text/unicode",str, str.data.length * 2);
var clipid=Components.interfaces.nsIClipboard;
if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
}
Can you please suggest what changes I need to do in above function so that works perfectly on safari as well as opera.
I know there is way if we install shockwave on these browsers using flash to get copied data. I just don't want to go for flash solution.
Thanks.
Best Regards,
Manoj

Categories

Resources