Firefox doesn't close WebSocket immediately on connection error? - javascript

I have this demo code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body onload="init()">
<script type="text/javascript">
function init() {
this.ws = new WebSocket("ws://localhost:1234");
console.log("opening");
// on close, display message and try to reopen
this.ws.onclose = function (evt) {
console.log("closed");
}
}
</script>
</body>
</html>
Which is connecting to a websocket endpoint that doesn't exist. I would expect this to error out basically immediately, but in Firefox, the time between it printing "opening" and printing "closed" is over a minute:
Is this expected behavior, or can it be configured to time out more quickly? In Chrome the connection errors out immediately:

Found it. This is intentional behavior to comply with RFC 6455. Per this patch, it uses an exponential backoff up to 60 seconds max. Unfortunately I think this means auto-reconnecting to a websocket endpoint in Firefox is broken.

Related

Console output sequence sometimes does not match with the sequence in which they are called

I am new to JavaScript and this is my first question on
Stackoverflow, please let me know if I missed something which
should be included in the question.
Question 1:
Can anyone explain why sometimes the console output sequence does not match with
the sequence in which they are called (myFunction(); and
messageLogger();).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Page</title>
</head>
<body>
<script>
let myFunction = function messageLogger() {
console.log('Message Logged');
}
myFunction(); //Should output message in console
messageLogger(); //Should generate error in console
</script>
</body>
</html>
This is Basic Function Expression code as shown above and in the image Original Code Image.
This code should show error in console after running myFunction(); as in image Console Output Image 1, but sometimes show's error before running myFunction(); as in image Console Output Image 2.
I haven't seen the problem after I updated the MS Edge Browser to Chromium version. I have no reason and solution to actual cause but issue cannot be seen anymore.

Document.write and windows.location together in IE is not working

I'm having a strange behavior and I'm fighting with myself in order to figure out what error can be.
I have an HTML page that, on body load, checks for the presence of a Variable "TRANSACTIONVALUE" in query string.
If the string is present and has specific value (8374), user is redirected to a specific page (let's call it "backend page").
Due that sometimes this "backend page" is a bit slow (it fires an agent that sometimes goes down for inactivity, so need to wait to reactivate it), i resolved it just adding a document.write that says just "Request in course... Please wait" in the same blanked page before the redirect.
My code is working for ALL browser, except for Internet Explorer where users are blocked in the first statement "document.write".
<!doctype html>
<html lang="us">
<head>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="TEXT/HTML;CHARSET=UTF-8">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT="-1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<script>
function CheckHeaderStatus() {
if (getQueryVariable("TRANSACTIONVALUE")==8374)
{
document.write("Request in course... Please wait");
window.location.href='http://backendapplication.goto.com/Default.aspx?TARGET='+TRANSACTIONVALUE;
}
}
</script>
</head>
<body onload="CheckHeaderStatus()" class="text-center">
<div> any text <div>
</body>
</html>
What happens is that it works for Chrome, Safari, Firefox. In these cases if an user has the TRANSACTIONVALUE=8374, on body load he can see the "Request in course... Please wait" message and then is redirect to the page http://backendapplication.goto.com/Default.aspx?TARGET='+TRANSACTIONVALUE
Now, in Internet Explorer users with TRANSACTIONVALUE=8374 can see ONLY the "Request in course... Please wait" message. The page stop to go head and so they are not redirect to the other page.
What can be the problem?
I have tested other way to obtain the same (as proven of my work) like:
"Remove all content from the page, create a div where I can write the massage of waiting, and then redirect, so:
while (document.firstChild) {
document.removeChild(document.firstChild);
};
// create new DIV
var newDiv = document.createElement("div");
newDiv.innerHTML = "<h1>Request in course... Please wait</h1>";
window.location.href='http://backendapplication.goto.com/Default.aspx?TARGET='+TRANSACTIONVALUE;
but again, it works for all browser expect for IE. I tested with IE9,10,11.
I'm missing something or it is a problem of IE?
Thanks all!!!
I have resolved in this way:
function CheckHeaderStatus() {
if (getQueryVariable("TRANSACTIONVALUE")==8374)
{
Redirect();
function Redirect() {
document.write("Request in course... Please wait");
window.location.href='http://backendapplication.goto.com/Default.aspx?TARGET='+TRANSACTIONVALUE;
}
}}
In this way it works also for IE :-)

IE 11 Permission Denied Javascript

I have created a trivial HTML/JS/Angular page (below) which when first rendered in IE 11, throws a Permission Denied exception. See images of the debugger console below. My IE version is 11.0.9 on Windows 7.
[EDIT] See source below. Meta tag included, but this does not resolve the issue. txs
<!DOCTYPE html>
<html ng-app="ie11bug">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IE 11 Bug</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
</head>
<body>
<div ng-controller="AppController">
Hello {{pageTitle}}
</div>
<script>
var app = angular.module('ie11bug', []).controller('AppController', function($scope) {
$scope.pageTitle = "This will not appear when page is 1first loaded";
});
</script>
</body>
</html>
I have tried various versions of Angular from 1.4 to 1.6, same result. After clearing the cache and loading the page the error occurs. If I then reload, the page successfully renders.
The error always seems to be thrown when the factory method (called from the $exceptionHandler) assigns the $log service into the cache.
I also get Permission denied when simply trying to log to the console
<script>
console.log("hello");
</script>
Any help/ideas would be appreciated.

HTML5 GeoLocation not working with Android

My device is a HTC One X, browser is Chrome. I'm trying to get HTML5 GeoLocation to work in browser however I'm unable to, it works on iOS and desktop however nothing for my Android. Below is my code:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
<meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'>
<title>HTML5 Test</title>
<script src='http://cloud.keepiteasy.net/libs/modernizr.custom.89661.js' type='text/javascript'></script>
<script src='http://cloud.keepiteasy.net/libs/jquery.js' type='text/javascript'></script>
</meta>
</head>
<body>
<script type="text/javascript">
$(function() {
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
}
function success(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
alert(lat);
alert(lng);
}
function error(err) {
if(err.code == 1) {
alert("Error: Access is denied!");
}else if( err.code == 2) {
alert("Error: Position is unavailable!");
}
}
});
</script>
</body>
</html>
UPDATE: I fixed the doctype
UPDATE: I updated the error function
UPDATE: On my HTC I am still getting nothing, not even an error. On my Nexus 7 (just tried it), it works fine... WTF, hardware issue? But other GPS based apps work...
I got the same issue on my HTC One X. At least you can make sure, your error function gets called, by adding a timeout:
navigator.geolocation.getCurrentPosition(success, error, {timeout:3000});
In this example, your error function gets called after 3 seconds.
Seems to be a hardware issue of some sort as no website can acquire my geolocation on my OneX, however my Nexus 7 the above code works fine.
Just restart your phone, guys. Yeah it's kinda stupid but it's the solution.

IE8 strange behavior, my problem or bug?

OK guys this is intreting,
I'm testing this page
http://static.nemesisdesign.net/demos/ie8-strange/test.html
on IE8 / windows XP.
This is the code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="content-language" content="en">
<title>Test</title>
</head>
<body>
<div id="thisgivesmeanerror">test</div>
<script>
thisgivesmeanerror = 'test';
alert('this alert won\'t be fired on my IE8, what about yours?');
</script>
</body>
</html>
If I open this page with IE8 I get an error.
If I change the code of the script to:
<script>
// note that I added var prefix
var thisgivesmeanerror = 'test';
alert('this alert won\'t be fired on my IE8, what about yours?');
</script>
It works fine.
This happens only on IE 7/8, didn't test it on IE6.
What do you think?
Does it happen to you also? Or is it just my browser that has gone crazy?
Addition
You're saying that is just not using the var prefix that cause the error?
I'm sorry guys but you're wrong, you didn't take time to test the code.
I uploaded a test2 page
http://static.nemesisdesign.net/demos/ie8-strange/test2.html
with the follwing cocde
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="content-language" content="en">
<title>Test 2</title>
</head>
<body>
<div id="thisgivesmeanerror">test</div>
<script>
thisdoesntgiveanyerror = 'test';
alert('This alert will be fired correcly');
</script>
</body>
</html>
That works fine.
So what is actually causing the error? The variable name without the var prefix having the same name as the ID of the DIV element.
Isn't this strange?
May be the answers to this SO-question can help?
You should always precede your variable declarations with var to specify their scope or you might observe inconsistent behavior between different browsers.
use var to declare variables instead of just plugging their name
I'd say the JavaScript interpreter in IE is slightly stricter than on FireFox and others, meaning the script returns an error when it comes to the variable definition line. Putting var in will ensure it actually is a variable.
It's very good practice to declare all your variables with var
James
EDIT
I can't get to IE at the moment, but I can recommend you change your <script> tag to <script type="text/javascript">.

Categories

Resources