Why does this JavaScript work on Safari, but not Firefox? - javascript

I have HTML file. I tried the code on Safari and it was working fine. But when I tried this on Firefox, it’s not working.Can anyone suggest how to make it working on firefox?
On click on undo button I want to retrieve contents from the jsp file. Thats working when I used this code on safari on my mac.. but when I open the same file using firefox its not working. I am not sure is it due to browser settings or due to some other reason. I checked browser setting of firefox 3.6.12 installed on mac also it is enabled for javascript and java...
When I checked on HTTPfox it showed in Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED) in the contents
Can anyone suggest whats going wrong???

XMLHttpRequests only work when the request is on the same domain as the JavaScript making the request. So, your call to xmlHttp.open() would only work if that HTML file was hosted on csce.unl.edu.

Can the ubuntu box access the url http://csce.unl.edu:8080 ? It may be network/proxy/firewall settings on the virtual machine or in Firefox settings.

I'd try running firefox on the Mac and see where that takes me. If that doesn't work Then the problem is the browser, if it does, it's the way you are loading the site

Use JQuery. It has an AJAX library which does these browser compatibility checks for you.
Also, Firebug may come in handy, to see whether the request is being sent and see what the response is.

I opened firebug > console and pasted
var xmlHttp, handleRequestStateChange;
handleRequestStateChange = function() {if (xmlHttp.readyState==4 && xmlHttp.status==200) { var substring=xmlHttp.responseText; alert(substring); } }
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://csce.unl.edu:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77540ceb", true);
xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null);
And i saw everything working. What the error exactly? Can you open firebug and look at javascript errors.
Edit try this:
var req = new XMLHttpRequest();
req.open('GET', '/');
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200)
alert(req.responseText);
else
alert("Error loading page\n");
}
};
req.send(null);

Related

$_REQUEST array empty on IE11 (windows 8 version 11.0.9600.17937) from xmlHttpRequest, works on Firefox and Chrome

I have looked thoroughly on google and stack overflow's history but nothing has fixed my issue.
I have a login feature to my website application which works on Firefox and Chrome with no problem. It also works on IE for every version but one specific version: 11.0.9600.17937 which is for windows 8. My customers are the one who have notified me of this because I have windows 7 and there is no problem on my version of IE11 so I cannot test against the actual issue without asking them to try.
From what I have debugged it appears that my variables are not being passed to the login page via xmlhttprequest because it is not passing my validations to test if the variables are passed in. Once again the validations pass in Firefox and Chrome.
Here is my xmlhttprequest in Javascript:
function sendUser(form)
{
var busloginID=document.forms[0].busloginID.value;
var buspassword=document.forms[0].buspassword.value;
var userData = [busloginID, buspassword, 'BUS'];
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("login").innerHTML=xmlhttp.responseText;
if(document.getElementById("loginSuccess"))
{
form.submit();
}
}
}
xmlhttp.open("GET","login.php?userData="+userData,true);
xmlhttp.send();
}
And here is the code receiving the xmlhttprequest on login.php:
$data = $_REQUEST['userData'];
$userDataArray = explode(",", $data);
$loginID=$userDataArray[0];
$password=$userDataArray[1];
$originFlag=$userDataArray[2];
These variables are not getting passed on this version of IE11, only this specific version.
Any suggestions are appreciated, I feel like I have tried everything.
Thanks
Jake
I substituted $_GET for $_REQUEST when requesting the variables on login.php and the user having the problem stopped experiencing issues. Not sure why that would fix it so it was either the substitution or user error by the client.

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.

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

Porting Issue - Internet Explorer to Everything Else

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();
}});

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