davej Posted March 11, 2015 Share Posted March 11, 2015 fnd and init are both functions so why we did not write them like this document.getElementById('btn1').onclick = fnd();window.onload = init(); Because that doesn't work. Try it and see for yourself. <script> 'use strict';x = 3.14; function show[]{return x +10 ;} </script> I see errors: SyntaxError: missing ( before formal parameters ReferenceError: assignment to undeclared variable x <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><script>window.onerror = function(a, b, c, d){alert('Javascript Error:n'+a+'nURL: '+b+'nLine: '+c+' Column: '+d);return true;}</script><script>'use strict';x = 3.14;function show[]{return x +10 ;}</script></head><body></body> </html> Link to comment Share on other sites More sharing options...
hisoka Posted March 11, 2015 Author Share Posted March 11, 2015 Sorry but I did not ask what is the difference between : document.getElementById('btn1').onclick = fnd();window.onload = init(); and this : document.getElementById('btn1').onclick = fnd;window.onload = init; my question was why we did not use the parenthesis in document.getElementById('btn1').onclick = fnd;window.onload = init; this answer "This line:window.onload = init;Tells it to assign the function init to the window.onload handler. This line:window.onload = init();Tells it to execute the function init and assign the return value of that function to the window.onload handler. If init does not return a function then that will be a problem." is about the logic that drives the code . However , my question was about the syntax and Syntactically , why we used init in window.onload = init; and fnd in document.getElementById('btn1').onclick = fnd; without parenthesis ?? "Are you sure about that?" yes I am sure I ran this script : <script>'use strict';x = 3.14; function show[]{return x +10 ;} </script> I did not include this : window.onerror = function(a, b, c, d){alert('Javascript Error:n'+a+'nURL: '+b+'nLine: '+c+' Column: '+d);return true;} I ran it only like this : <script>'use strict';x = 3.14; function show[]{return x +10 ;} </script> It shew no error Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2015 Share Posted March 11, 2015 is about the logic that drives the code . However , my question was about the syntax andOK, if you're only asking about the syntax then the parentheses tells it to execute the function. It shew no errorWell then that's a problem, because putting "[]" after a function name is a syntax error in all versions of Javascript. If you don't see that syntax error then maybe you're not looking in the correct place for error messages. Link to comment Share on other sites More sharing options...
davej Posted March 12, 2015 Share Posted March 12, 2015 Obviously you aren't going to see an error unless you use an onerror codeblock or you look in the browsers javascript console. Link to comment Share on other sites More sharing options...
hisoka Posted March 12, 2015 Author Share Posted March 12, 2015 "OK, if you're only asking about the syntax then the parentheses tells it to execute the function." again my question was not what is the role of the parenthesis my question was : Syntactically , why we used init in window.onload = init; and fnd in document.getElementById('btn1').onclick = fnd; without parenthesis ?? in other words , why init in : window.onload = init; is used without parenthesis why fnd in document.getElementById('btn1').onclick = fnd is used without parenthesis in other words syntactically , why init in window.onload = init is like this : window.onload = init and not like this window.onload = init() and the same for fnd ?? "you don't see that syntax error then maybe you're not looking in the correct place for error messages" what do you mean ? I read the content of the link you gave from the beginning to the end and I did as the page says . However , I forget to say that I have mozilla version 36.0.1 and may be it does not support strict mode "Obviously you aren't going to see an error unless you use an onerror codeblock" it is not written in the strict mode tutorial page you gave me that I should use "onerror codeblock" Link to comment Share on other sites More sharing options...
davej Posted March 12, 2015 Share Posted March 12, 2015 You have been talking about using the browser console for weeks now. Have you never seen a Javascript error in the browser console? The onerror codeblock is a non-standard item that I add to my own code but most developers simply use the browser developer console. https://developer.chrome.com/devtools/docs/console https://developer.mozilla.org/en-US/docs/Tools/Browser_Console https://msdn.microsoft.com/en-us/library/dd565625%28v=vs.85%29.aspx Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2015 Share Posted March 12, 2015 in other wordssyntactically , why init in window.onload = init is like this :window.onload = init and not like this window.onload = init() and the same for fnd ??I've already explained it both syntactically and semantically. Go back and review my answers. If you do not know what effect adding the parentheses has, then look that up also.I have mozilla version 36.0.1 and may be it does not support strict modeIt does. Link to comment Share on other sites More sharing options...
hisoka Posted March 13, 2015 Author Share Posted March 13, 2015 "I've already explained it both syntactically and semantically. Go back and review my answers. If you do not know what effect adding the parentheses has, then look that up also." this is what you wrote to me : "OK, if you're only asking about the syntax then the parentheses tells it to execute the function." this is the reformulation of your answer : the parentheses tell it (tell this window.onload and thisdocument.getElementById('btn1').onclick ) to execute the function . All what you did in your answer is telling me the role of the parentheses did I ask about the role of the parentheses so that you answer me the parentheses tells it to execute the function??! if I did then please show me where (copy paste) I asked the contrary : first question : Posted 11 March 2015 - 11:21 PM Syntactically , why we used init in window.onload = init; and fnd in document.getElementById('btn1').onclick = fnd; without parenthesis ?? same question : Posted Yesterday, 03:07 PM: why init in window.onload = init; is used without parenthesis ? why fnd in document.getElementById('btn1').onclick = fnd is used without parenthesis ? I was asking why they are used without parentheses and you answered : then the parentheses tells it to execute the function Link to comment Share on other sites More sharing options...
hisoka Posted March 13, 2015 Author Share Posted March 13, 2015 "Have you never seen a Javascript error in the browser console?" if you look to my previous posts you will find the answer to your question . But I will refresh your memory a little http://w3schools.invisionzone.com/index.php?showtopic=52653&hl=http://w3schools.invisionzone.com/index.php?showtopic=52574&hl= Link to comment Share on other sites More sharing options...
hisoka Posted March 13, 2015 Author Share Posted March 13, 2015 window.onload = init; is used without parenthesis why fnd in document.getElementById('btn1').onclick = fnd is used without parenthesis In JavaScript , there are what we call event handlers . Event handlers are little codes (methods) used to tell the browser what to do when an event occurs. the syntax of an event handler is like this [element].[event]=[function] element is a property of the document event is the event that will occur like clicking the mouse function is the function that will run once the event occurs. Now I will answer this question : window.onload = init; is used without parenthesis why fnd in document.getElementById('btn1').onclick = fnd is used without parenthesis it is simple because init or fnd without parentheses is a reference to a function and not a function . In other word init without parentheses is a reference a pointer to the function init() in function init() {document.getElementById('btn1').onclick = fnd;} and fnd without parentheses is a pointer or a reference to the function fnd() in function fnd(){document.write("writing this text overwrites the entire document");} Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2015 Share Posted March 13, 2015 Good lord. You asked what it means syntactically. When you add the parentheses, it executes the function. So guess what it does when you don't add the parentheses? Do you think it executes the function?I cannot state it any more clearly. So, I'll just paste what I wrote before. This line:window.onload = init;Tells it to assign the function init to the window.onload handler. This line:window.onload = init();Tells it to execute the function init and assign the return value of that function to the window.onload handler.That's pretty simple. That is what happens both with and without parentheses. If you're looking for something other than an explanation of what happens if you use parentheses, and also if you don't, then I don't know what you're asking.Here's a tip: if you are asking for free help from volunteers, and you are not getting the answers that you expect, then maybe you're not asking the right questions. Link to comment Share on other sites More sharing options...
hisoka Posted March 13, 2015 Author Share Posted March 13, 2015 (edited) your answers are good . Especially yours . They are an inspiration for me and especially yours . Continue like this . Give me all what you know . It is always useful what you wrote . "and you are not getting the answers that you expect" I expect only one thing : To understand what is going on regardless of the answer . All answers are useful . What so ever is my reaction to them . It is and it will be in an instructive and scientific way . If I make an error . Correct me . :) Edited March 13, 2015 by hisoka Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now