How to use ActiveXobject to support in other browsers using JavaScript? - javascript

Currently, the below-sepecified javascript function is working fine with IE and the requirement is that I need to use the same function to display the content in other browsers too.
I'm aware that IE only supports ActiveX controls.
Could anyone please suggest the replacement of this function in javascript.
function show_packagemappings(target_letter) {
// Load XML
xml = new ActiveXObject("Msxml2.DOMDocument.6.0");
xml.async=false;
xml.load("xmldata\\map_packages.xml");
xslt = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");
xslt.setProperty("AllowDocumentFunction",true);
xslt.async=false;
xslt.load("packagemappings.xsl");
xslTemp = new ActiveXObject("Msxml2.XSLTemplate.6.0");
xslTemp.stylesheet = xslt;
xslProc = xslTemp.createProcessor();
xslProc.input = xml;
xslProc.addParameter("target_letter", target_letter);
// Transform
xslProc.transform;
document.open()
document.write(xslProc.output)
document.close()
}

function show_packagemappings(target_letter)
{
if (target_letter == '???') {
document.getElementById("divElement").innerHTML = "";
sessionStorage.target_letter = "";
return;
}
sessionStorage.target_letter = target_letter;
fn = "xmldata\\map_packages.xml"
// console.log(fn);
// code for IE
if (window.ActiveXObject || "ActiveXObject" in window) {
// Load XML
xml = new ActiveXObject("Msxml2.DOMDocument.6.0");
xml.async=false;
xml.load(fn);
if (xml.getElementsByTagName("target_letter")[0] == null) {
var text = "<target_letter id='" + target_letter + "'>"
+ "<LastUpdate></LastUpdate>"
+ "</target_letter>";
var parser = new DOMParser();
xml = parser.parseFromString(text,"text/xml");
}
console.log(xml.getElementsByTagName("target_letter")[0].getAttribute('id'));
xslt = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");
xslt.setProperty("AllowDocumentFunction",true);
xslt.async=false;
xslt.load("packagemappings.xsl");
xslTemp = new ActiveXObject("Msxml2.XSLTemplate.6.0");
xslTemp.stylesheet = xslt;
xslProc = xslTemp.createProcessor();
xslProc.input = xml;
xslProc.addParameter("target_letter", target_letter);
// Transform
xslProc.transform;
document.getElementById("divElement").innerHTML = xslProc.output;
}
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
xhttp = new XMLHttpRequest();
xhttp.open("GET", fn);
// via Cache-Control header:
xhttp.setRequestHeader("Cache-Control", "no-cache, no-store, max-age=0");
// fallbacks for IE and older browsers:
xhttp.setRequestHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
xhttp.setRequestHeader("Pragma", "no-cache");
// Force the response to be parsed as XML
xhttp.overrideMimeType('text/xml');
xhttp.onload = function () {
var xml;
if (xhttp.readyState === xhttp.DONE && xhttp.status === 200) {
xml = xhttp.responseXML;
} else {
var text = "<target_letter id='" + target_letter + "'>"
+ "<LastUpdate></LastUpdate>"
+ "</target_letter>";
var parser = new DOMParser();
xml = parser.parseFromString(text,"text/xml");
}
xhr = new XMLHttpRequest();
xhr.open("GET", "packagemappings.xsl");
// Force the response to be parsed as XML
xhr.overrideMimeType('text/xml');
xhr.onload = function () {
if (xhr.readyState === xhr.DONE && xhr.status === 200) {
var xsl = xhr.responseXML;
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
xsltProcessor.setParameter(null,"target_letter",target_letter);
resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("divElement").innerHTML = "";
document.getElementById("divElement").appendChild(resultDocument);
}
}
xhr.send("");
};
xhttp.send("");
}
}

Related

Javascript not working as expected for the function if record exist also

I have a js code which returns me a datatable count when I add the doc no
But even if the record exist it still goes into the else part
Here is the code:-
function getOtherDBInward() {
StrPriFnName = "FunGetOTHERDBInward(" + document.getElementById('TxtInwardNo').value + ")";
var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
ObjPriXMLHTTP.open("GET", "FrmInwardXMLHTTP.aspx?para=" + StrPriFnName, false);
ObjPriXMLHTTP.send("");
if ((ObjPriXMLHTTP.responseText) != "") {
var StrPriData = ObjPriXMLHTTP.responseText.split('~');
document.getElementById('checkDBMkey').value = StrPriData[0];
document.getElementById('checkDBMDocNo').value = StrPriData[1];
}
else {
alert("ERROR: Document does not exist");
return false;
}
}
Also see the function
public static string FunGetOTHERDBInward(object[] args)
{
string StrPriRetVal = "";
DataAccessLayer ObjPriDal = new DataAccessLayer(Providers.ConfigDefined);
DataTable Dt1 = new DataTable();
Dt1 = ObjPriDal.ExecuteDataTable("Select mkey, doc_no from erp190516.dbo.inward_doc_tracking_hdr where doc_no='" + args[0].ToString().Trim() + "'");
if (Dt1.Rows.Count > 0)
{
StrPriRetVal += Dt1.Rows[0]["mkey"].ToString().Trim() + "~" + Dt1.Rows[0]["doc_no"].ToString().Trim();
}
return StrPriRetVal;
}
NOTE
I get as unspecified error at line
var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
Just replace
var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
With
var ObjPriXMLHTTP ;
if(window.XMLHttpRequest)
ObjPriXMLHTTP = new XMLHttpRequest;
else
ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
Since if (window.XMLHttpRequest) detects whether the browser has XMLHttpRequest functionality if so the XMLHttpRequest object is constructed that way. i.e code for IE7+, Firefox, Chrome, Opera, Safari
Otherwise the code blindly assumes it can create the XMLHttpRequest by calling ActiveXObject functions i.e code for IE
for more detail Create an XMLHttpRequest Object

How to make the data downloaded same across all the versions of browsers?

I am downloading a file in my java application using jquery.
It is downloading in correct format in IE8 where as Format is getting changed when i am using IE10.
Here is my code, where I am setting hearder.
function converthtml() {
var CustomerID = document.getElementById('customerID').value;
var ci = document.getElementById("ci").value;
var strvar;
strvar = term.textContent || term.innerText;
strvar = encodeURIComponent(strvar);
var xmlHttp;
var xmlHttp;
var flag = 0;
if (typeof XMLHttpRequest != "undefined") {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp == null) {
alert("Browser does not support XMLHTTP Request");
return;
}
xmlHttp.onreadystatechange = stateChange2;
function stateChange2() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
//alert("response : "+xmlHttp.responseText);
alert("Session value saved successfuly");
}
}
var params = "sessiondata=" + strvar + "&ci=" + ci + "" + "&customerID=" + CustomerID;
xmlHttp.open("POST", "DownloadSession", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(params);
// xmlHttp.send("sessiondata="+strvar+"");
document.getElementById("downloadSession").disabled = false;
}
I suspect the following statement is causing the format change
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
Kindly suggest me an alternative that keeps content-type same across all the browser versions......

XSLT is working only Firefox

Im new to XSLT. I am getting a XML file from return value of ECHO from PHP.
Then I try to transform to XSL but it only works in Firefox.
When I run the Javascript debugger from Chrome it says
:
Uncaught TypeError: undefined is not a function maintenance.js:30
getData
[Added: the OP eventually noted that line 30 of maintenance.js reads:
function generateReport() {
xHRObject.open("GET", "getXML.php", rue);
xHRObject.onreadystatechange = getData; //this is where i get debug error in chrome
xHRObject.send(null);
}
I have broken this into multiple lines for legibility.]
But when I run firebug with same code in Firefox no error and can get the result I want.
var xhr = false;
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
function getData()
{
if ((xhr.readyState == 4) &&(xhr.status == 200))
{
if (window.ActiveXObject)
{
var xml = xhr.responseXML;
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false; xsl.load("abc.xsl");
var transform = xml.transformNode(xsl);
var frame = document.getElementById("frame");
frame.innerHTML = transform;
}
else
{
var xsltProcessor = new XSLTProcessor();
xslStylesheet = document.implementation.createDocument("", "doc", null);
xslStylesheet.async = false;
xslStylesheet.load("abc.xsl");
xsltProcessor.importStylesheet(xslStylesheet);
xmlDoc = xhr.responseXML;
var fragment = xsltProcessor.transformToFragment(xmlDoc, document);
document.getElementById("frame").innerHTML = new XMLSerializer().serializeToString(fragment);
}
}
}
function test()
{
xhr.open("GET", "getXML.php", true);
xhr.onreadystatechange = data;
xhr.send(null);
}

Trouble with having global JavaScript variables updated

I am trying to get a XML document sorted and decided to go for the "sort via XSLT" approach.
However, I am having trouble updating my two global variables that should contain the content of the XML and XSLT files and I can't really figure out why.
Up until now I never had this kind of problem and global variables used to work... I also didn't declare them inside the functions, but used the global name instead and also tried using window.variable, but to no avail.
Does anyone have an idea why the code doesn't update the global variable?
best regards,
daZza
<script type="text/javascript">
var xml = "";
var xsl = "";
function callSort()
{
loadSortXML();
loadSortXSLT();
sortXML();
}
function loadSortXML()
{
var xmlHttp = null;
var xmlData;
var xmlFile = "data/LessonsLearned.xml";
if (typeof XMLHttpRequest != 'undefined')
{
xmlHttp = new XMLHttpRequest();
}
if (!xmlHttp)
{
try
{
xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e)
{
xmlHttp = null;
}
}
}
if (xmlHttp)
{
var url = xmlFile;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
xml = xmlHttp.responseXML;
}
}
xmlHttp.send();
}
}
function loadSortXSLT()
{
var xmlHttp = null;
var xmlData;
var xmlFile = "data/xslt.xml";
if (typeof XMLHttpRequest != 'undefined')
{
xmlHttp = new XMLHttpRequest();
}
if (!xmlHttp)
{
try
{
xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e)
{
xmlHttp = null;
}
}
}
if (xmlHttp)
{
var url = xmlFile;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
xsl = xmlHttp.responseXML;
}
}
xmlHttp.send();
}
}
function sortXML()
{
console.log("XML " + xml);
console.log("XSL "+ xsl);
var parser = new DOMParser();
var domToBeTransformed = parser.parseFromString(xml, "text/xml");
var xslt = parser.parseFromString(xsl, "text/xml");
var processor = new XSLTProcessor();
processor.importStylesheet(xslt);
var newDocument = processor.transformToDocument(domToBeTransformed);
var serializer = new XMLSerializer();
var newDocumentXml = serializer.serializeToString(newDocument);
alert(newDocumentXml);
}
</script>

Ajax code control

Sorry for that stupid question but, is this code right because it seems to be broken.
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
var nocache = 0;
function vloz() {
var kom= encodeURI(document.getElementById('komen').value);
var site_name = encodeURI(document.getElementById('user_id').value);
var p_id = encodeURI(document.getElementById('p_id').value);
var zed = encodeURI(document.getElementById('zed').value);
nocache = Math.random();
http.open('get', 'kmnt.php?site_url='+kom+'&site_name=' +site_name+'&site='+p_id+'& zed='+zed+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
}
}
I have a form when i am sending komen, user_id, p_id and zed
Not sure if this is it but it appears that you have one too many curly braces in the beginning of your code:
if(browser == "Microsoft Internet Explorer") {
request_type = new ActiveXObject("Microsoft.XMLHTTP");
} else {
request_type = new XMLHttpRequest();
}
return request_type;
// } <- extra one here

Categories