Porting Issue - Internet Explorer to Everything Else - javascript

I have some code which was developed on a Windows 7 computer and runs any Windows 7 compeer without any hiccups. I tried running it on my Mac and the program just stays on the loading page.
The program displays the bing maps view and loads a few things in order to get the location of a particular satellite. Now all the maths and stuff works but I think the problem lies here:
function getOrbitalElements()
{
TLE_Line1="";
TLE_Line2="";
pgTXT = "";
xmlhttp = null;
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange = stateChange;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
}
So is there any way that this can be changed to run on any browser? Thanks
P.S. If you need to see the entire code I'll add it

There are no ActiveX objects on Mac. The following line won't work:
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
You could use XMLHttpRequest:
var xmlhttp = null;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
...
}
But if you are seeking best cross browser support I would recommend you using a javascript framework such as jQuery to perform your AJAX requests.

Replace
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
with
xmlhttp = window.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
EDIT
As others have said, you might find benefit in using something like jQuery (which is very good) but you may not need to take the effort to adjust your existing code just yet. If you have written good (standards) javascript, you will find the browser cross-compatibility issues should be minimal.
In the future, be sure to test in other browsers early and often to avoid this kind of problem.

The best way to port a particular web app from a Browser specific version to a browser agnostic one is to use a javascript framework like jQuery. It's designed to smooth out the rough edges that come up between different browsers into a friendly + unified API. For example the above code could be done across multiple browsers with a jQuery ajax request
$.ajax({
url: url,
success: function () {
// Called when the query completes
statechange();
}});

Related

How to replace active x control form javascript project

I am currently working on javascript project which uses backbone.js as the framework. its a pretty old project where Activex components are used in it, now i need to replace this controls completely so that even when i have my active x disabled my functionality should work as expected. How can i achieve this ? Here is my below chunk of a code which can be refereed . Thank you
function getXMLRequest() {
var xmlHttp = null;
if (location.protocol.substr(0, 4) == "http") {
try { // Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) { // Internet Explorer
try {
// i need to replace these activex controls
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
} else {
xmlHttp = new XMLCclRequest();
}
return xmlHttp;
}
That code only uses ActiveX if the browser has no native support for XMLHttpRequest.
i.e. If the browser is Internet Explorer 7 or earlier.
Nobody should be using that browser today. It doesn't get security updates. It doesn't run on operating systems that get security updates.
So:
function getXMLRequest() {
return new XMLHttpRequest();
}
If you need to support such horrifically obsolete browsers, then you'll need to change the code on server and client to use JSONP.

Googlebots causes JS errors, should I care?

On production server, I watch JS errors and send to the server:
<script type="text/javascript">
window.onerror=function(msg,url,line) {
if (window.XMLHttpRequest)
{
var xmlhttp = new XMLHttpRequest();
}
else
{
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.open('POST', '/logJSerrorsHere', true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send('msg='+encodeURIComponent(msg)+'&url='+encodeURIComponent(url)+'&line='+line);
return true;
}</script>
and sometimes it logs some "mysterious" errors: "$ is not defined", but all of them comes from "googlebot(at)googlebot.com" or spiderbot. Should I deal with it?
Depends :) If your site is readable and indexable with out Javascript (and your site is visible in search) I wouldn't worry too much about it, unless you feel the error is indicative of a bigger issue. You can test this using Fetch and Render in Google Webmaster Tools.
If your site relies on Javascript to render the page (i.e. it uses AngularJS for example) then yes, fix it.

What should be the ideal fallback method?

I am using Ajax to call for a special file if JavaScript is enabled and if it is disabled then it loads the regular file.
My Code is like this:
window.onload = function() {
document
.getElementById("wrapper")
.innerHTML = "<img src='cdn/img/demo/loading.gif'>";
var x = null;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
var x = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// code for IE6, IE5
var x = new ActiveXObject('MSXML2.XMLHTTP.3.0');
} else {
// #TODO - Fallback
// My Question : What should be an ideal Fallback method here?
}
x.open("GET", "js_enabled.php", true);
x.send("");
x.onreadystatechange = function() {
if (x.readyState == 4) {
if (x.status == 200)
document.getElementById("wrapper").innerHTML = x.responseText;
else
document.getElementById("wrapper").innerHTML = "Error loading document";
}
}
}
What should be an ideal Fallback method in the first block?
UPDATE:
The fallback is for the browsers. As you see that those commands are for IE and normal browsers. I want to know if god forbid some browser does not understand ant of those 2 commands then what?
AJAX is extremely likely to be available if JS is available, so it's not something you'll have to deal with often, but if you're looking for a fallback when AJAX isn't available (which I'm guessing you are from your code example), then the only real way of doing that is making sure that your page works fine without it.
Links should point to appropriate pages (or just to reloading the current page) and then be overwritten with the JS to use AJAX (but only if AJAX is available). That way you'll have a functioning site even if JS isn't available, let alone if AJAX isn't.
To give you some idea of how small the problem is when it comes to lack of AJAX support, according to this site browsers that include AJAX support include the following:
Internet Explorer 5.0 and up
Opera 7.6 and up
Netscape 7.1 and up
Firefox 1.0 and up
Safari 1.2
As you can see, that's some pretty old stuff.
you are confused if javascript is disabled then there will be no fallback method. Assuming that you mean anabling/disabling of javascript.
Then what you do is fill up the area with default text and hide it using js. so if JS isnt enabled the default text will be shown to the user.
and if js is enabled the default text will be hidden almost as soon as it is rendered and then your ajax thing will take over

XMLHttpRequest.open does not work with IE9 (though XMLHttpRequest object is supported) [duplicate]

var xhttp=new XMLHttpRequest();
xhttp.open('GET', 'foo.xml', false);
F12 pops back: SCRIPT5: Access is denied. on Line 95, which is the xhttp.open line.
My JavaScript seems well-formed, and Firefox does what I think it should.
I've read a lot of questions very similar to this one, so I've checked out the Same Origin Policy, but I can't see how it'd apply considering foo.xml is in the same directory as the html file. I opened up the scripting permissions on my local intranet, and told McAfee to take a five-minute break, just to be sure. I even tried running IE as admin, so this can't really be a permissions issue can it? Why else would IE be denied access to a local file?
Maybe you like to check the links below:
Making cross domain JavaScript requests using XMLHttpRequest or XDomainRequest
XMLHttpRequest – Mozilla Developer Network
A good summary of the jQuery x-domain requests
Which browser supports x-domain?
You likely have a Mark-of-the-Web on the local file. See http://blogs.msdn.com/b/ieinternals/archive/2011/03/23/understanding-local-machine-zone-lockdown-restricted-this-webpage-from-running-scripts-or-activex-controls.aspx for an explanation.
This example illustrate how to use AJAX to pull resourcess from any website. it works across browsers. i have tested it on IE8-IE10, safari, chrome, firefox, opera.
if (window.XDomainRequest) xmlhttp = new XDomainRequest();
else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", "http://api.hostip.info/get_html.php", false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText.split("\n");
var IP = false;
for (i = 0; hostipInfo.length >= i; i++) {
if (hostipInfo[i]) {
ipAddress = hostipInfo[i].split(":");
if (ipAddress[0] == "IP") {
IP = ipAddress[1];
}
}
}
return IP;
On IE7, IE8, and IE9 just go to Settings->Internet Options->Security->Custom Level and change security settings under "Miscellaneous" set "Access data sources across domains" to Enable.
This error message (SCRIPT5: Access is denied.) can also be encountered if the target page of a .replace method is not found (I had entered the page name incorrectly). I know because it just happened to me, which is why I went searching for some more information about the meaning of the error message.
Most likely, you need to have the Javascript served over SSL.
Source: https://www.parse.com/questions/internet-explorer-and-the-javascript-sdk
I think that the issue is that the file is on your local computer, and IE is denying access because if it let scripts have access to files on the comp that the browser is running on, that would be a HUGE security hole.
If you have access to a server or another comp that you could use as one, maybe you could try putting the files on the that, and then running the scripts as you would from a website.
Probably you are requesting for an external resource, this case IE needs the XDomain object. See the sample code below for how to make ajax request for all browsers with cross domains:
Tork.post = function (url,data,callBack,callBackParameter){
if (url.indexOf("?")>0){
data = url.substring(url.indexOf("?")+1)+"&"+ data;
url = url.substring(0,url.indexOf("?"));
}
data += "&randomNumberG=" + Math.random() + (Tork.debug?"&debug=1":"");
var xmlhttp;
if (window.XDomainRequest)
{
xmlhttp=new XDomainRequest();
xmlhttp.onload = function(){callBack(xmlhttp.responseText)};
}
else if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
Tork.msg("Response:"+xmlhttp.responseText);
callBack(xmlhttp.responseText,callBackParameter);
Tork.showLoadingScreen(false);
}
}
xmlhttp.open("POST",Tork.baseURL+url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data);
}
I had faced similar issue on IE10. I had a workaround by using the jQuery ajax request to retrieve data:
$.ajax({
url: YOUR_XML_FILE
aync: false,
success: function (data) {
// Store data into a variable
},
dataType: YOUR_DATA_TYPE,
complete: ON_COMPLETE_FUNCTION_CALL
});
$.ajax({
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
alert(location.ip);
}
});
This code will work https sites too
Open the Internet Explorer Developer Tool,
Tools -> F12 developer tools. (I think you can also press F12 to get it)
Change the Document Mode to Standards. (The page should be automatically refresh, if you change the Document Mode)
Problem should be fixed.
Enjoy

SCRIPT5: Access is denied in IE9 on xmlhttprequest

var xhttp=new XMLHttpRequest();
xhttp.open('GET', 'foo.xml', false);
F12 pops back: SCRIPT5: Access is denied. on Line 95, which is the xhttp.open line.
My JavaScript seems well-formed, and Firefox does what I think it should.
I've read a lot of questions very similar to this one, so I've checked out the Same Origin Policy, but I can't see how it'd apply considering foo.xml is in the same directory as the html file. I opened up the scripting permissions on my local intranet, and told McAfee to take a five-minute break, just to be sure. I even tried running IE as admin, so this can't really be a permissions issue can it? Why else would IE be denied access to a local file?
Maybe you like to check the links below:
Making cross domain JavaScript requests using XMLHttpRequest or XDomainRequest
XMLHttpRequest – Mozilla Developer Network
A good summary of the jQuery x-domain requests
Which browser supports x-domain?
You likely have a Mark-of-the-Web on the local file. See http://blogs.msdn.com/b/ieinternals/archive/2011/03/23/understanding-local-machine-zone-lockdown-restricted-this-webpage-from-running-scripts-or-activex-controls.aspx for an explanation.
This example illustrate how to use AJAX to pull resourcess from any website. it works across browsers. i have tested it on IE8-IE10, safari, chrome, firefox, opera.
if (window.XDomainRequest) xmlhttp = new XDomainRequest();
else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", "http://api.hostip.info/get_html.php", false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText.split("\n");
var IP = false;
for (i = 0; hostipInfo.length >= i; i++) {
if (hostipInfo[i]) {
ipAddress = hostipInfo[i].split(":");
if (ipAddress[0] == "IP") {
IP = ipAddress[1];
}
}
}
return IP;
On IE7, IE8, and IE9 just go to Settings->Internet Options->Security->Custom Level and change security settings under "Miscellaneous" set "Access data sources across domains" to Enable.
This error message (SCRIPT5: Access is denied.) can also be encountered if the target page of a .replace method is not found (I had entered the page name incorrectly). I know because it just happened to me, which is why I went searching for some more information about the meaning of the error message.
Most likely, you need to have the Javascript served over SSL.
Source: https://www.parse.com/questions/internet-explorer-and-the-javascript-sdk
I think that the issue is that the file is on your local computer, and IE is denying access because if it let scripts have access to files on the comp that the browser is running on, that would be a HUGE security hole.
If you have access to a server or another comp that you could use as one, maybe you could try putting the files on the that, and then running the scripts as you would from a website.
Probably you are requesting for an external resource, this case IE needs the XDomain object. See the sample code below for how to make ajax request for all browsers with cross domains:
Tork.post = function (url,data,callBack,callBackParameter){
if (url.indexOf("?")>0){
data = url.substring(url.indexOf("?")+1)+"&"+ data;
url = url.substring(0,url.indexOf("?"));
}
data += "&randomNumberG=" + Math.random() + (Tork.debug?"&debug=1":"");
var xmlhttp;
if (window.XDomainRequest)
{
xmlhttp=new XDomainRequest();
xmlhttp.onload = function(){callBack(xmlhttp.responseText)};
}
else if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
Tork.msg("Response:"+xmlhttp.responseText);
callBack(xmlhttp.responseText,callBackParameter);
Tork.showLoadingScreen(false);
}
}
xmlhttp.open("POST",Tork.baseURL+url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data);
}
I had faced similar issue on IE10. I had a workaround by using the jQuery ajax request to retrieve data:
$.ajax({
url: YOUR_XML_FILE
aync: false,
success: function (data) {
// Store data into a variable
},
dataType: YOUR_DATA_TYPE,
complete: ON_COMPLETE_FUNCTION_CALL
});
$.ajax({
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
alert(location.ip);
}
});
This code will work https sites too
Open the Internet Explorer Developer Tool,
Tools -> F12 developer tools. (I think you can also press F12 to get it)
Change the Document Mode to Standards. (The page should be automatically refresh, if you change the Document Mode)
Problem should be fixed.
Enjoy

Categories

Resources