Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do different net browsers display different output for this piece of code?

Tags:

javascript

 <html>
    <head><title>Frontpage</title></head>
    <h3>Hello</h3>

    <script>
	var a=1;
	function myFunc(){
	document.write(a+"  ");
	a=a+1;
	myFunc();
    }
    </script>

    <body><button type="button"  onclick="myFunc()">Hey there</button></body>
    </html> 

The output is from 0 to 53075 in Internet explorer while it is 12561 in Chrome.I don't understand why different browsers display different outputs and how it stops the recursion.Thank You.

like image 532
Deba Avatar asked Jan 16 '18 07:01

Deba


People also ask

Why do different browsers display content differently?

Why Does my Website Look Different in internet explorer? Websites are made up of a set of instructions spoken in a web code language, most often HTML or CSS. Often, different browsers interpret code languages differently, which results in different interpretations.

Do different browsers show different results?

Different browsers often interpret or display website source code like HTML and CSS in slightly different ways, resulting in the same website looking and feeling different accordingly.

Does all browsers read HTML codes the same way?

A: The simple answer: use whatever browser you like. HTML and CSS are industry standards, which means that all browsers try to support HTML and CSS in the same way (just make sure you are using the newest version of the browser for the best support).

Why is CSS different in web browsers?

It look different because each browser has his own CSS style defined. This styles apply to the HTML markup when no other CSS is defined inline or comes from an external CSS file. That's the reason why a lot of websites using a "Reset. css".


2 Answers

That's a recursive function, and IE and Chrome have different Javascript rendering engines. (Chrome uses V8). More than likely, it's some unimportant details about the internals of the particular engine. In this case related to stack size (how many times something can be recursively called before it 'blows up').

Another thing to think about, is that it's not so common for regular programs to go so deep into a call stack, unless you're poking around or doing it on purpose, like your example above.

When I say "deep", I'm referring to the fact an evaluated statement (your function) must return a value, and if it doesn't do that (and instead calls another nested function), it must evaluate that first. In this way, it's going "deeper." In your example, it never is able to get a return value of anything until it runs out of space because the innermost function never returns anything.

Here's another example:

add(1, add(5, add(10, 20)))

In this example of pseudocode, the add function takes 2 arguments, 2 numbers to add. The 2nd argument needs to be evaluated first, before it can get the return value (so it can add it to 1). So... it 'pauses' execution of 1 + ... and then runs the second add function, which does the same thing. Now you have 2 functions which are 1 + ..., basically waiting on "hold". Finally, the innermost function DOES return a value, 30, so that gets passed out to the 5 + 30 which is 35, and now that 35 gets passed to the remaining waiting 1 + ... function to complete the computation.

This can be thought of have a stack depth of 3 (to put it simply).

Basically in your program, it doesnt get that final return value (to then pass up the call chain). Instead it just runs out of space, and dies. Therefore youre merely testing the limits of the various javascript engines and when they choose to die.

like image 148
Tallboy Avatar answered Oct 01 '22 18:10

Tallboy


Well , this code is infinity loop.

You must got this type of message :

RangeError: Maximum call stack size exceeded.

Every browser have own max number to allow this kind of executing code. I don't really know but a run two time and i got same number it mean that this case is strict denied with some of max value.

like image 45
Nikola Lukic Avatar answered Oct 01 '22 18:10

Nikola Lukic