...

View Full Version : Why is my JavaScript code WORKING?



Peeyush
05-17-2012, 04:11 PM
I havent even declared x,y and z as variables!



x=10;
y=20;
z = x+y;
document.getElementById('good').innerHTML=z;


I am getting so confused... since the time i started learning JS i thought declaring variables (var x,y,z;) was real important!

Philip M
05-17-2012, 04:51 PM
The var keyword is technically optional (but it is strongly recommended that it should be used). If a variable is declared outside a function then it has global scope whether or not the var keyword is used. If the variable is declared within a function then if the var keyword is used then the variable is local to that function only, and is not available to other functions in the script. If the variable is declared within a function without the var keyword then it is global in scope. In general global variables should be avoided as far as possible. So yes, it is really important to use the var keyword.


<script type="text/javascript">

var a = 10; // global
b = 12; // also global

function dosums() {
var d = 35; // local to this function only;
c = 15; // global
}

dosums(); // to initiate c and d

alert (a);
alert (b);
alert (c);
alert (d);

</script>


In theory, there isn't any difference between the theory and practice. In practice, there is.

VIPStephan
05-17-2012, 05:06 PM
And note that JavaScript’s strict mode (https://developer.mozilla.org/en/JavaScript/Strict_mode#Converting_mistakes_into_errors) “global variables” defined without the var keyword are errors (i. e. they aren’t allowed to be declared like that).

Peeyush
05-18-2012, 04:02 AM
The var keyword is technically optional (but it is strongly recommended that it should be used). If a variable is declared outside a function then it has global scope whether or not the var keyword is used. If the variable is declared within a function then if the var keyword is used then the variable is local to that function only, and is not available to other functions in the script. If the variable is declared within a function without the var keyword then it is global in scope. In general global variables should be avoided as far as possible. So yes, it is really important to use the var keyword.


<script type="text/javascript">

var a = 10; // global
b = 12; // also global

function dosums() {
var d = 35; // local to this function only;
c = 15; // global
}

dosums(); // to initiate c and d

alert (a);
alert (b);
alert (c);
alert (d);

</script>


In theory, there isn't any difference between the theory and practice. In practice, there is.

Why do you, and many programmers on these forums suggest that global variables shall be avoided? sorry i am new to this level of programming....

Peeyush
05-18-2012, 04:08 AM
And yeah, why avoid tables? i have heard this sooo many times - 'you must avoid tables'.. anything else i must avoid?

felgall
05-18-2012, 06:32 AM
Why do you, and many programmers on these forums suggest that global variables shall be avoided? sorry i am new to this level of programming....

About one in ten JavaScript questions I see is someone asking why adding two scripts to the same page means that one or both don't work properly while either one will worjk by itself. The reason is always that they are trying to use the same global for different purposes.

By keeping the number of globals to a minimum you reduce the possibility of a conflict. For example the only thing that the latest version of my marquee script has that is global is one class name - you put that class name on each block element in the page you want to have scroll and the script does the rest. Only if you were to try to apply a different script that tried to interact with the same elements in a different way would you have a conflict.

Provided that all of your JavaScript is together and not scattered through the HTML you can effectively set up a separate namespace for all the variables by wrapping the entire script inside an anonymous function.


(function() {
// your javascript here
})();

The new strict version of JavaScript gives better control over what variables are local and which are global by requiring that all variables be declared. The old version of JavaScript makes a variable global if you use if without declaring it. The reason for this change is that it helps to enforce you deciding on which scope each variable is declared in instead of ending up with some accidentally global because you forgot to declare them (which is often the reason for conflicts).

If you look at any of the JavaScript frameworks you will find that they generally have only one or two global variables - for example prototype only has $() in the global scope and jQuery only has jquery() and $() in the global scope (with one as an alias of the other and with the ability to turn off the alias if it conflicts - such as if you are silly enough to want to use prototype and jQuery in the same page).

felgall
05-18-2012, 06:38 AM
And yeah, why avoid tables? i have heard this sooo many times - 'you must avoid tables'.. anything else i must avoid?

It isn't so much that you should avoid tables - instead it is a matter of using the right tags for what the content is. Using a table identifies the content as tabular data and so you should use a table when the content is tabular data and not use a table when it isn't - just as you use a paragraph tag when the content is a paragraph and don't use a paragraph tag when the content is a list or a heading.

The easiest way to tell if the content really belongs in a table is to consider whether it will still lokk acceptable in those mobile browsers that always display a border around table cells regardless of what you specify in your page.

If you want table like behaviour for the appearance then all current browsers except IE7 can provide that using display:table, display:table-row and display:table-cell in your CSS.

As for other things to avoid - as far as JavaScript is concerned I have listed all the main ones (together with reasons why) at http://javascriptexample.net/badjs.php

Peeyush
05-18-2012, 06:49 AM
Hey thanks, here is a sample code, could you tell me which of the variables are global and which local? it will help me understand better:



var a,b,c;

a = 1;

var f = 10;

function main()
{
var g;
b=3;
d=4;
var e =5;
var c = 6;
}
function other()
{
g = 60;
}

Peeyush
05-18-2012, 06:52 AM
your site says that JS in head is bad. am i doing things rightly?



<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>List Of</title>
<link href="stylef.css" rel="stylesheet" type="text/css" />
<script src="javaf.js" type="text/javascript" ></script>
<link href="xmlf.xml" rel="alternate" type="application/rss+xml" />
</head>

felgall
05-18-2012, 07:13 AM
Hey thanks, here is a sample code, could you tell me which of the variables are global and which local? it will help me understand better:



var a,b,c;

a = 1;

var f = 10;

function main()
{
var g;
b=3;
d=4;
var e =5;
var c = 6;
}
function other()
{
g = 60;
}


There are global variables - a, b, c, d, f, and g.
Variables local to main() are - c, e, g (note that the c and g are different variables from the global ones of the same name so the global ones would need to be accessed as window.c and window.g from within that function).
The other() function has no local variables.

felgall
05-18-2012, 07:27 AM
your site says that JS in head is bad. am i doing things rightly?



<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>List Of</title>
<link href="stylef.css" rel="stylesheet" type="text/css" />
<script src="javaf.js" type="text/javascript" ></script>
<link href="xmlf.xml" rel="alternate" type="application/rss+xml" />
</head>


Browsers will delay rendering the page until any JavaScript files that have started downloading have finished - just in case they contain document.write statements. So by requesting JavaScript files early you slow the page showing up in the browser. Also if the script doesn't use document.write it will likely be referencing elements within the page and so you don't want the script to run before the elements it references are loaded. Also at the bottom of the page most scripts can run straight away instead of having to wait for all the other files in the page to finish loading.

So placing the script at the bottom means the page renders faster and the script runs sooner.

Philip M
05-18-2012, 07:28 AM
About one in ten JavaScript questions I see is someone asking why adding two scripts to the same page means that one or both don't work properly while either one will worjk by itself. The reason is always that they are trying to use the same global for different purposes.


There are in fact two reasons why two scripts may conflict:-

a) duplicate global variable and/or function names (including loop counters etc.)
b) multiple onload statements.

The issue of conflicting global variables can be dealt with by assigning meaningful names to the variables rather than a, b and c. In other words, a descriptive name such as customerFirstName is less likely to conflict.

felgall
05-18-2012, 07:49 AM
Yes technically that is true, but the amount of javascript I write is small and users, even on slow dialup connections, would not notice the difference if my js was in the head or in the body.

So I normally put the js in the head so I don't have to scroll down to the bottom of the page to get to it.

All the JavaScript I now write consists of one or at most two script tags at the bottom of the HTML - with all the code in an external file. So there generally isn't a need to scroll the HTML, I just go straight to the js file. If you are working with JavaScript the only time you need to touch the HTML at all is to add the script tag.

It is no harder to add the tag at the bottom than it is to add it at the top and so no reason not to get that the benefits even if they will be too small to notice - when two alternatives are equally easy then there's no reason for using the one that slows things down by even a microsecond. Anyway, it is most likely that you will need more JavaScript if you attach it at the top to get the script to wait for all the ids it references to load first so loading the script at the bottom can also make the amount of code you need to write less - making that the far easier option as well.

The scripts I write would need to have extra code added if you put them at the top of the page as they are designed to interact with HTML elements already rendered in the page at the time the script loads.

For example the latest version of my marquee script:


// Continuous Text Marquee
// copyright 30th September 2009, 2nd May 2012 by Stephen Chapman
// http://www.felgall.com
// permission to use this Javascript on your web page is granted
// provided that all of the code below in this script (including these
// comments) is used without any alteration

(function(speed,cl) {"use strict";var nodes, i, ii, mqr, objwidth, mq, mqRotate;mqr = [];if (!document.getElementsByClassName) document.getElementsByClassName = function(cl) {var retNode, myclass, elem, classes, i, ii;retnode = [];myclass = new RegExp('\\b'+cl+'\\b');elem = this.getElementsByTagName('*');for (i = 0, ii = elem.length; i < ii; i++) {classes = elem[i].className;if (myclass.test(classes)) retnode.push(elem[i]);}return retnode;};objWidth = function(obj) {return obj.offsetWidth || obj.clip.width;};mq = function(node){var wid, fulwid, txt, heit, maxw, s;this.mqo=node;txt = this.mqo.innerHTML;this.mqo.style.position = 'relative';this.mqo.style.overflow = 'hidden';s = document.createElement('span'); this.mqo.appendChild(s);this.mqo.getElementsByTagName('span')[0].style.whiteSpace = 'nowrap'; this.mqo.getElementsByTagName('span')[0].innerHTML = txt;wid = objWidth(this.mqo.getElementsByTagName('span')[0])+ 5;fulwid = objWidth(this.mqo);this.mqo.innerHTML = '';heit = this.mqo.style.height;this.mqo.onmouseout=function() {mqr[0].TO=setInterval(function() {mqRotate(mqr);},speed)};this.mqo.onmouseover=function() {clearInterval(mqr[0].TO);};this.mqo.ary=[];maxw = Math.ceil(fulwid/wid)+1;for (var i=0;i < maxw;i++){this.mqo.ary[i]=document.createElement('div');this.mqo.ary[i].innerHTML = txt;this.mqo.ary[i].style.position = 'absolute';this.mqo.ary[i].style.left = (wid*i)+'px';this.mqo.ary[i].style.width = wid+'px';this.mqo.ary[i].style.height = heit;this.mqo.appendChild(this.mqo.ary[i]);}return this.mqo;};mqRotate = function(mqr){ var j, i, x, y, z, maxa;for (j=mqr.length - 1; j > -1; j--) {maxa = mqr[j].ary.length;for (i=0;i<maxa;i++){x = mqr[j].ary[i].style;x.left=(parseInt(x.left,10)-1)+'px';}y = mqr[j].ary[0].style;if (parseInt(y.left,10)+parseInt(y.width,10)<0) {z = mqr[j].ary.shift();z.style.left = (parseInt(z.style.left,10) + parseInt(z.style.width,10)*maxa) + 'px'; mqr[j].ary.push(z);}}};nodes = document.getElementsByClassName(cl);for (i = 0, ii = nodes.length; i < ii; i++) {mqr.push(new mq(nodes[i]));};if (mqr.length>0)mqr[0].TO=setInterval(function() {mqRotate(mqr);},speed);
})(50,'marquee');

That will interact with any <div class="marquee"> tag that appears before the script tag that attaches the script but will ignore any <div class="marquee"> tag that comes after the <script> tag. To get it to work at the top of the page you'd need to add a delay until the HTML finishes loading (or the entire page for those browsers that don't recognise when the DOM finishes loading.

Philip M
05-18-2012, 08:42 AM
Take no notice of iBall! He only repeats what others have already contributed in order to increase his post count. His reputation has now progressed beyond "iBall can only hope to improve" to "iBall is infamous around these parts". How true. :D I think he holds the world's record for the number of times he has been banned from this forum.

felgall
05-18-2012, 10:02 AM
that's ok :) If you prefer to put all your js at the bottom that's fine by me.

I prefer to put mine in the head normally for the reasons I posted before.

It isn't a matter of preference - it is a matter of brevity of code and ease of maintenance. You didn't post any actual reason for placing your scripts in the head of the page other than personal preference and a total disregard for the visitors to your page.

There are four points at which you might want to run JavaScript in a web page.

1. As soon as possible before the page renders. Here you must place the script in the head. An example of such a script is:


if (top.location != self.location && uri.args) top.location = self.location;

I am still looking for a second example of a script that needs to go in the head that isn't a variant of that script.

2. As soon as possible after the element it interacts with has loaded. The point where such a script would run quickest is with the script tag immediately after that of the element it references. This would jumble the JavaScript with the HTML and the improved maintainability of attaching the script to the bottom rather than immediately after the element more than makes up for the microscopic time saving. If you put such a script in the head you need to make it a function triggered by a domready event (something not all browsers support) - anyway that means more code in the script than is needed if you put the script after the element it interacts with.

3. After the entire page has loaded - unless your page has lots of huge images this will not be noticeably later than when the HTML finishes loading. So for most cases you can get away with simply including the script at the bottom and not bothering to test if all the images have loaded (if the script uses lots of images then you should consider using just in time loading where you load the images as the script runs). This means that by placing your script at the bottom of the page you get rid of almost all situations where a window onload = event handler is needed and in the rare situation where you really do need to wait you can use the slightly longer event listener equivalent and so avoid one of the JavaScript beginner's biggest conflict issues completely.

4. In response to an event - with these scripts the page will have loaded long before the script is called so it doesn't matter where you attach it but the page will still load slightly faster with it at the bottom and with all your other scripts at the bottom it makes little sense to put it anywhere else.

As I have found only one example of the first of these and no situations where the third applies and since the second saves a lot of code when the script is at the bottom there is no reason why you should place any script other than that one example I posted anywhere but at the bottom of your HTML. After all with the script in a separate file there will not be any reason to go to the script tag anyway - you'd make any changes to the JavaScript in the JavaScript file - not in the HTML file.

With all the issues beginners have with clashes between multiple onloads where simply placing the scripts at the bottom does away with the need for onloads almost completely I am starting to consider adding onload to my list of the bad bits of JavaScript that should almost never be used. I really need to find an example of where it is needed though first so that I can give that as an example of where it should be used. I haven't come across a script that can't be rewritten to get rid of it though so I am still looking.

felgall
05-18-2012, 11:16 AM
The amount of javascript I write is small

not if you are putting it in the head it isn't - in the head you need extra code so it doesn't run before the part of the page it interacts with has loaded. Move your scripts to the bottom and you will be able to make the script a lot smaller than it currently is.

I am not telling you your pages load slower because that is insignificant - I am telling you that your scripts are way bigger than they need to be because you are attaching them in the wrong spot and so need extra code to compensate.

In fact I would challenge anyone to post a script that they have attached to the head of their page and I will post a smaller version that attaches to the bottom of the page that does exactly the same thing (provided of course that it is written using proper 21st Century JavaScript intended to run in modern browsers and isn't written in that antiquated 20th Century JavaScript required by Netscape 4 and earlier).

felgall
05-18-2012, 11:25 AM
tell me the extra code. I'd like to see how much extra there is.

Show me one of your scripts and I'll show you what it would look like without the extra code.

Peeyush
05-18-2012, 11:55 AM
i hav a question. In what cases do you need to attach more than 1 external js sheets to your code?

Peeyush
05-18-2012, 12:50 PM
and @ felgall, you can use my code. even i am interested in making my code short. here is only one function. if your technique applies only on more than one code then you can tell me i can give you bigger code...



function addto()
{
// txt = b ;
ma = document.getElementById("tb1");
mb = ma.insertRow(-1);
mc = mb.insertCell(0);
md = mb.insertCell(1);
me = mb.insertCell(2);
mf = mb.insertCell(3);
mb.id = k + "mb" + i;
mc.id = k + 'mc' + i ;
md.id = k + 'md' + i ;
me.id = k + 'me' + i;
mf.id = k + 'mf' + i;
mc.innerHTML=k+'a'+i;
md.innerHTML=k+'a'+i;
me.innerHTML=k+'a'+i;
}

VIPStephan
05-18-2012, 01:11 PM
i hav a question. In what cases do you need to attach more than 1 external js sheets to your code?

That can happen if you include external widgets or whatever. All these Facebook, Twitter, Google+ buttons you see are scripts that you just copy and paste into your HTML. They all are retreiving some JS from a different server and/or define and execute JS right where the scripts are included. Scripts from these more professional companies are usually written in a way that they donít conflict but there are thousands of free scripts out there (for example countdown timers are among the more popular) that arenít always very compatible.

Also, if you use a JS framework they usually have plugins that can extend the core functionality, and these plugins usually come in their own files. Now, you could add them to the file where the core framework is but thatís not always feasible.

felgall
05-18-2012, 08:39 PM
With more than one external script your code at the bottom of the page might read:


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript" src="myjqueryscript.js"></script>
<script type="text/javascript" src="http://google-analytics.com/ga.js"></script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-99999999-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body></html>

Note that where sites provide antique JavaScript using document.write as part of their functionality then you must put their script tag in the exact spot where you want their script to produce its output or it will not run properly - that's just one issue with such old but still very common scripts provided by sites who decided to not hire themselves any one who actually knows JavaScript to write those scripts for them.

felgall
05-18-2012, 08:47 PM
You''re the one making the claims about my code so I don't have to show you anything. If you want me to believe you, the onus is on you to prove to me what you say is correct for my code.


This proves what I said - your code is so bad that you don't want anyone to actually see any of it so as to see that there is a far better way to code it.

So stop suggesting that placing JavaScript in the head is the best place.

Anyway I'll make you the reverse offer. Take the marquee JavaScript I posted earlier in this thread that is designed to work at the bottom of the page and produce a version of your own that works from the head of the page without the code being any longer than in my script and without cheating by moving any part of the functionality currently in the JavaScript into either the HTML or CSS (since that would break the page if JavaScript were to be turned off).

felgall
05-18-2012, 08:50 PM
and @ felgall, you can use my code. even i am interested in making my code short. here is only one function. if your technique applies only on more than one code then you can tell me i can give you bigger code...



function addto()
{
// txt = b ;
ma = document.getElementById("tb1");
mb = ma.insertRow(-1);
mc = mb.insertCell(0);
md = mb.insertCell(1);
me = mb.insertCell(2);
mf = mb.insertCell(3);
mb.id = k + "mb" + i;
mc.id = k + 'mc' + i ;
md.id = k + 'md' + i ;
me.id = k + 'me' + i;
mf.id = k + 'mf' + i;
mc.innerHTML=k+'a'+i;
md.innerHTML=k+'a'+i;
me.innerHTML=k+'a'+i;
}


See http://javascriptexample.net/domtable09.php for ideas on how to make that shorter or http://javascriptexample.net/domtable12.php if you are looking to create an entire table.

VIPStephan
05-18-2012, 09:42 PM
To be honest, I also usually put my scripts in the head, I didn’t feel like that has any noticable impact so far. The problem with script-at-the-bottom setup was for me the flash of unstyled content (FOUC). You have to put at least one line of JS in the head to apply a class to the documentElement for different styling.

Now, since I’m working with jQuery very much my latest approach is to load the scripts asymmetrically (i. e. only when they are needed), plus having a local fallback:


<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
if(typeof jQuery==="undefined"){
var e=document.createElement("script");
e.src="js/jquery.min.js";
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e)
}
var f=document.createElement("script");
f.src="js/functions.js";
f.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(f)
</script>

The functions.js has all the custom JS I write and uses $.getScript() whenever a plugin is required.

felgall
05-18-2012, 11:00 PM
To be honest, I also usually put my scripts in the head, I didnít feel like that has any noticable impact so far.

Yes but jQuery contains a big chunk of code specifically to handle having the script load at the top of the page before any of the HTML it is to interact with has loaded. If you weren't using jQuery then you'd need to add all that extra code to your own script. In both cases it makes the script bigger than it would need to be if it were added to the bottom of the page - the difference with jQuery is that if you were to load all your scripts at the bottom and delete the part of jQuery that is now unnecessary then you wouldn't be able to use a cached copy and then your page would be delayed in loading for everyone while that large script library loads - even if they have JavaScript turned off.

So when using a framework that supplies the extra code to handle the script being loaded first there is no significant difference between attaching them all in the head or attaching them all at the bottom. With your own code without a framework the benefit is that you have less code to maintain with the script at the bottom.

The thing with iBall's statement about placing scripts in the head is that he doesn't do that himself. His scripts have the JavaScript scattered throughout the HTML rather than in a separate file. Keeping all the JavaScript in a separate file and only having script tags in the HTML to link them together makes a huge difference to the maintainability of the web page and the reusability of the script. iBall's scripts avoid the need for the extra code in the head by placing part of the JavaScript lower in the page so as to avoid the extra overhead of having all the script in the head - that's why he couldn't provide an example head script - because that's not how he writes his JavaScript.


flash of unstyled content (FOUC)

That can't be caused by JavaScript - JavaScript doesn't affect the appearance of the page - only CSS does that. The only way JavaScript might have 'fixed' it is if the script applied styles while the CSS was still loading.

VIPStephan
05-19-2012, 12:27 AM
That can't be caused by JavaScript - JavaScript doesn't affect the appearance of the page - only CSS does that. The only way JavaScript might have 'fixed' it is if the script applied styles while the CSS was still loading.

No, JS is adding a class to the html or body element which CSS is using to apply styles specifically if JS is enabled (such as when you have a home page slideshow and need to hide all slides except the first one while without JS all slides are shown by default). If the script was at the end of the body then the class (and the styles, respectively) is only added after the HTML (DOM) has loaded completely while you could actually add it to the html element immediately if it was in the head Ė and this becomes especially noticeable when the page is huge and there is much HTML to load.

felgall
05-19-2012, 01:17 AM
No, JS is adding a class to the html or body element which CSS is using to apply styles specifically if JS is enabled (such as when you have a home page slideshow and need to hide all slides except the first one while without JS all slides are shown by default). If the script was at the end of the body then the class (and the styles, respectively) is only added after the HTML (DOM) has loaded completely while you could actually add it to the html element immediately if it was in the head Ė and this becomes especially noticeable when the page is huge and there is much HTML to load.

That's a variant of what I said - the JavaScript isn't the direct cause in that situation, it is the update to the HTML that affects how the stylesheet is applied which causes the problem.

There is an HTML 5 solution for this type of problem:


<noscript><link type="text/stylesheet" ...></noscript>

that allows you to override styles when JavaScript is not available allowing you to include the class in the HTML in the first place and style it differently depending on whether the script will be able to run or not.

In HTML 4 the <noscript> tag can only be used in the body of a web page where it serves no purpose since wrapping it around a link tag in the head is the only place where it can actually do something useful and in HTML 4 that tag isn't valid in the only part of the page where it would serve a useful purpose.

Since Netscape 4 dies there has been no reason for the <noscript> tag to be used in the body of a web page as it is a block level tag and you can use JavaScript to apply the same changes to any part of the body even individual characters without needing to use noscript.

Old Pedant
05-19-2012, 05:54 AM
This is almost funny:
http://www.codingforums.com/showpost.php?p=1230032&postcount=17

Here's iBall following Felgall's advice. The script is indeed at the bottom of the page.

And, in point of fact, most of the JS code he has posted recently has been placed at the bottom of the page.

Why do I get the feeling he is arguing for the sake of arguing???

Philip M
05-19-2012, 07:13 AM
Why do I get the feeling he is arguing for the sake of arguing???

Because that is what does! Argumentative and bellicose is his lifestyle. His reputation has now progressed beyond "iBall can only hope to improve" to "iBall is infamous around these parts". How true. I think he holds the world's record for the number of times he has been banned from this forum.

felgall
05-19-2012, 08:30 AM
Why do I get the feeling he is arguing for the sake of arguing???

Certainly seems that way. There is no real benefit to anyone reading the threads to see his arguments because he doesn't back them up with any facts - just his own personal preference. It's a bit hard to have a meaningful discussion when there are no actual facts presented to support the opposing view.

Entirely different from some of the discussions I have had with you and Philip where we present opposing views and give actual reasons so that those reading can choose whichever viewpoint corresponds to their situation.

Peeyush
05-19-2012, 08:57 AM
guys i have a question, suppose i put my script at the bottom of the page, html has loaded, js is loading. Now the user clicks a button as html is loaded, and button, when clicked calls a function. Now there are 3 cases.

1) function hasnt loaded
2) function hasnt COMPLETELY loaded
3) the functions these functions is calling havnt loaded

what will happen in each of the cases?

Peeyush
05-19-2012, 09:44 AM
<offtopic>
if iBall has put Philip M on his ignore list, then iBall can't see Philip M's posts, but can Philip M see iBall's posts? If not, can he see when iBall's posts when some1 quotes his post?

Peeyush
05-19-2012, 11:07 AM
what about Philip M? can he see your posts?

Lerura
05-19-2012, 12:21 PM
Nice twist!

The thread have gone from being a question regarding declaration of variables, to a discussion about ignore lists and where to place the scripts.

Philip M
05-19-2012, 04:20 PM
what about Philip M? can he see your posts?

Yes, and iBall can see mine with a couple of mouse clicks. :) Or by browsing without logging in.

As I have said, there is a dim-witted bird called an emu which hides its head in the sand, believing that if it cannot see its enemies then its enemies cannot see it. :)

You should take no notice of iBall! He only repeats what others have already contributed in order to increase his post count. His reputation has now progressed beyond "iBall can only hope to improve" to "iBall is infamous around these parts". How true. I think he holds the world's record for the number of times he has been banned from this forum.

Peeyush
05-19-2012, 09:26 PM
Is it just me or philip m is really repeating last few lines again n again? Anyways i am still looking for answer to these:

guys i have a question, suppose i put my script at the bottom of the page, html has loaded, js is loading. Now the user clicks a button as html is loaded, and button, when clicked calls a function. Now there are 3 cases.

1) function hasnt loaded
2) function hasnt COMPLETELY loaded
3) the functions these functions is calling havnt loaded

what will happen in each of the cases?

felgall
05-19-2012, 11:28 PM
guys i have a question, suppose i put my script at the bottom of the page, html has loaded, js is loading. Now the user clicks a button as html is loaded, and button, when clicked calls a function. Now there are 3 cases.

1) function hasnt loaded
2) function hasnt COMPLETELY loaded
3) the functions these functions is calling havnt loaded

what will happen in each of the cases?


1) if the function hasn't loaded then neither has the code that follows it to attach it to the button - so the default action of the button when JavaScript isn't available will run instead - generally loading a new page (if the button only works with JavaScript then it should not have been added to the page until the JavaScript is loaded).

2) the code to attach the function to the button comes after the function and so will not attach the function until after all the code it calls is loaded.

3) will never happen since attaching the JavaScript to the button comes last in the JavaScript after everything else has loaded.

See http://javascriptexample.net/domform12.php for an example of how to attach functions to form fields. If JavaScript isn't loaded yet then the real action represented in the example by action="#" would run to process the form completely ignoring the JavaScript because it isn't there yet. All form fields including buttons need to be inside a form if you code them in the HTML in order that the action can define how to process the form fields without JavaScript.

The alternative is to use JavaScript to add the button to the page so that it only appears once the code that it runs has loaded. Of course then it has to be for something where it doesn't matter if 5 or 10% of your visitors don't even know it is there because they don't have JavaScript.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum