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.
Related
I'm trying to make an XMLHttpRequest in the options page of an extension. In my options.js file I simply have the following code :
if (window.XMLHttpRequest){
var xhr = new getXMLHttpRequest();
}
But I have this error in the console
Uncaught ReferenceError: getXMLHttpRequest is not defined
I saw here that getXMLHttpRequests are a problem for hosted apps, but in this case, it's a simple extension, so I don't understand.
To construct an XHR object you use new XMLHttpRequest();.
getXMLHttpRequest is not a standard function.
I saw here that getXMLHttpRequests are a problem…
The question at the other end of the link doesn't use a function with a name starting with get.
You can use
function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
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
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();
}});
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
Before someone said that I did not read I may say that I read almost everything linked with my question. But I couldn't find my answer.
So, I have a simple AJAX script that loads my external file inside predefined div. This is the code of those script:
function loadTwitter()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your Browser Don't Support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById("column_twitter").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET","../includes/home/twitter.php",true);
xmlHttp.send(null);
}
It works just fine in everyone browser that I test (FF, Opera, Chrome, Safari), but inside IE7 don't want to inject my external php file into predefined div. It always stays the default text that I wright inside div...
And I think that the problem is in this row:
document.getElementById("column_twitter").innerHTML=xmlHttp.responseText;
So, any suggestions how to fix this for IE (7 and above)?
I think you'd be better off using a javascript framework such as jQuery that allows you to concentrate on getting your features implemented rather than browser compatibility and low level network interaction. Using jQuery you could simply do:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript">
$.get( '../includes/home/twitter.php', function(data) {
$('#column_twitter').html( data );
});
</script>
I know that this is an old question, but I ran into a similar thing today and I wanted to post it out for others in case you experience this issue. This is likely being caused by your "column_twitter" tag being embedded in multiple DIV statements or in a table. IE7 doesn't like this for some reason.
Good Luck!