www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: HELP: Simple HTML submit code

  1. #16
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Thats where scripts are meant to go so that as soon as the page is loading the scripts functions and data is set and ready for use, loading last means that you can run in to problems if theirs latency in a connection, thats why. IDK who told you that they have to go in the body or the foot, you only put scripts in those locations when you need to to prevent problems where putting the script in the head or external file causes problems.

    Using anonymous functions is really to be avoided and that is from the debug stand point, any errors will result in an error at line 0 even if your script is on line 200.

    Keeping things simple, using an array of objects presents fewer problems with inserting items with multiples and duplicates.

    .innerHTML is used because it works and does not pose any over head issues, generally you find this lame argument that one method is faster than another when something is "Supposed" to be retired, same type of argument has existed for the last 20 years which is why I don't buy in to that argument, if it exists and still works, use it. I would argue that it takes longer to parse (interpret) several lines of code than one line of code. I would say that the argument might hold some merit if JS was compiled code but its not, its interpreted line by line.

    If the item does not exist in my script example returns a zero length array which can in the follow up produce the desired result of informing that no results were found.

    As for the demo I posted...

    Its been kept simple because the OP is new to coding and the demo needs to be simple.

    The structure is simply to create the minimal functions needed, a data set thats and array "[]" that holds objects "{}" for its data.

    A form that allows the user to input some or part of a search that is case insensitive.

    If you want to stop clashes, you would use JSON over an anonymous function
    Code:
    var locations = {
    	data:[],
    	add_address:function(address,city,postcode){
    		// push an object in to the array
    		this.data.push({"address":address,"city":city,"postcode":postcode});
    	},
    	find_address:function(searchTerm){
    	for(f=0,result=[]; f<this.data.length; f++){
    		for( l in this.data[f] ){
    			var tmp = this.data[f][l].toLowerCase();
    			if( tmp == searchTerm.toLowerCase() || tmp.match( RegExp( searchTerm , "i" ) ) )
    				result.push(this.data[f]);
    			}
    		}
    	return result;
    	},
    	checkThis:function(o){
    		x = this.find_address( o.cityselection.value );
    		for(y=0,str=""; y<x.length; y++){
    			for(a in x[y]){
    				str += "<br>" + x[y][a]; 
    				}
    			str += "<br>";
    		}
    		str += "<hr>";
    	
    	document.getElementById("output").innerHTML = (x.length) ? str : "<br>Nothing Found<hr>";
    	return false;
    	}
    }
    
    locations.add_address("123 Wiggins St","Somecity1","ST 12345");
    locations.add_address("1 Address St","London","LL1 1LL");
    locations.add_address("123 Address St","London","ST 12345");
    locations.add_address("123 Address St","Somecity4","ST 12345");
    locations.add_address("123 Wiggings Street","Manchester","M4 4AB");
    locations.add_address("123 Address St","Somecity6","ST 12345");
    locations.add_address("123 Address St","Somecity7","ST 12345");
    Then a minor change in the form submission...

    HTML Code:
    <form name="city" action="javascript:;" onSubmit="return locations.checkThis(this);">
    You have a self contained element that won't clash with anything else going on and awareness of what variables in use should be down to the programmer to know what is being used in a project.

    For this exercise though, the OP needs to make it much more clear on the their needs.
    Last edited by \\.\; 07-20-2014 at 07:06 AM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  2. #17
    Quote Originally Posted by \\.\ View Post
    Thats where scripts are meant to go so that as soon as the page is loading the scripts functions and data is set and ready for use, loading last means that you can run in to problems if theirs latency in a connection, thats why. IDK who told you that they have to go in the body or the foot, you only put scripts in those locations when you need to to prevent problems where putting the script in the head or external file causes problems.
    They don't have to go there, but they usually load a hell of a lot faster since they can start loading as soon as the DOM hits them, and it will load all scripts in BODY before it even starts in on other files. Known speed booster, and I've never even heard of it being a problem -- and since Google pagesped and YUI tools are both advocating it....

    Quote Originally Posted by \\.\ View Post
    Using anonymous functions is really to be avoided and that is from the debug stand point, any errors will result in an error at line 0 even if your script is on line 200.
    What on earth garbage debugger are you using that would exhibit that behavior? IE9 or lower? NEVER seen that be a problem, the consoles in "real" Opera, ChroPera, Chrome AND Firefox all work just fine with them, as does IE10+... as does both firebug and dragonfly. Not sure where you got that idea...

    Quote Originally Posted by \\.\ View Post
    Keeping things simple, using an array of objects presents fewer problems with inserting items with multiples and duplicates.
    I was acting on the assumption that was a placeholder for AJAX of JSON data, since that seemed to be the OP's intent unless I REALLY misunderstood them. Much less having a monolithic mass of data like either of our examples would become uselessly large REALLY quick.

    Quote Originally Posted by \\.\ View Post
    .innerHTML is used because it works and does not pose any over head issues
    Does not post any overhead?!? On what planet?

    Quote Originally Posted by \\.\ View Post
    generally you find this lame argument that one method is faster than another when something is "Supposed" to be retired, same type of argument has existed for the last 20 years which is why I don't buy in to that argument, if it exists and still works, use it. I would argue that it takes longer to parse (interpret) several lines of code than one line of code. I would say that the argument might hold some merit if JS was compiled code but its not, its interpreted line by line.
    ... and there's the trap in your logic -- HTML has to be parsed too... so you've got JS that has to be parsed (as a giant memory wasting string) that then has to parsed A SECOND TIME from HTML into the DOM. Parse once vs parse twice?

    See what I'm saying?

    There ARE times where innerHTML is your best option -- like parsing a massive block; but if all you're doing is adding result sets, plugging into the dom will blow innerHTML out of the water -- try this with a result set of a 100 or so on a netbook or phone, and see what happens.

    Quote Originally Posted by \\.\ View Post
    Its been kept simple because the OP is new to coding and the demo needs to be simple.
    There's simple, and there's incomplete / outdated methodology. Remember the old addage, things should be as simple as they need to be, and no simpler.

    Quote Originally Posted by \\.\ View Post
    If you want to stop clashes, you would use JSON over an anonymous function
    Why put something in the global namespace if you don't have to? *SIGH* -- same reasoning I guess as the people who put mysql login info into DEFINE I guess. Of course, with JSON needing a massive parser unless you don't give a flying purple fish about IE9 and earlier, I really don't consider it a great answer... well, unless you meant just using a normal javascript object... which I did, but that does jack for any functions and variables you use in processing it.

    You seem REALLY misinformed on anonymous functions, innerHTML, and code loading order on browsers.

    Quote Originally Posted by \\.\ View Post
    For this exercise though, the OP needs to make it much more clear on the their needs.
    On that at least, we can agree.
    Last edited by deathshadow; 07-20-2014 at 07:50 AM.

  3. #18
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Quote Originally Posted by deathshadow View Post
    They don't have to go there, but they usually load a hell of a lot faster since they can start loading as soon as the DOM hits them, and it will load all scripts in BODY before it even starts in on other files. Known speed booster, and I've never even heard of it being a problem -- and since Google pagesped and YUI tools are both advocating it....
    Just because Google and YUI advocate something does not automatically make it right and for the record, JS starts loading faster in the head because it is read in to the browser first making it available faster.

    What on earth garbage debugger are you using that would exhibit that behavior? IE9 or lower? NEVER seen that be a problem, the consoles in "real" Opera, ChroPera, Chrome AND Firefox all work just fine with them, as does IE10+... as does both firebug and dragonfly. Not sure where you got that idea...

    I was acting on the assumption that was a placeholder for AJAX of JSON data, since that seemed to be the OP's intent unless I REALLY misunderstood them. Much less having a monolithic mass of data like either of our examples would become uselessly large REALLY quick.

    Does not post any overhead?!? On what planet?
    ? What are you blathering on about? For small data sets then fine, larger ones AJAX call to a server side, larger, AJAX to a server side querying a database. Unless the OP gives some clue as to the scope, is this a college assignment? Community project? We don't know until they make it clear what they are trying to achieve and to assume is a bit presumptive.
    ... and there's the trap in your logic -- HTML has to be parsed too... so you've got JS that has to be parsed (as a giant memory wasting string) that then has to parsed A SECOND TIME from HTML into the DOM. Parse once vs parse twice?

    See what I'm saying?
    No trap in logic at all, something I have noted over the many years of web coding, too many people with the idea that your coding has to conform to standards, utter rubbish, write what works best and if its not cross browser, code for it. Writing code because some stuffed shirt in some standards compliance department is saying so to justify their job can kiss my ass.
    There ARE times where innerHTML is your best option -- like parsing a massive block; but if all you're doing is adding result sets, plugging into the dom will blow innerHTML out of the water -- try this with a result set of a 100 or so on a netbook or phone, and see what happens.


    There's simple, and there's incomplete / outdated methodology. Remember the old addage, things should be as simple as they need to be, and no simpler.
    It work because it was designed to be used and adding and removing elements in the dom tree is costly in processing time, if you have a dongle like I use for internet access, you will notice that people who code JavaScript have pages that are not only cleaner but load faster than those that call upon a framework that loads and consumes memory to do simple things, a good example is Facebook, over use of AJAX and frameworks to produce a layout that would be far simpler if coded in the box method rather than creating and adding in elements to them fill with code.

    Do you see why creation of elements to post content is slower?

    Why put something in the global namespace if you don't have to? *SIGH* -- same reasoning I guess as the people who put mysql login info into DEFINE I guess. Of course, with JSON needing a massive parser unless you don't give a flying purple fish about IE9 and earlier, I really don't consider it a great answer... well, unless you meant just using a normal javascript object... which I did, but that does jack for any functions and variables you use in processing it.

    You seem REALLY misinformed on anonymous functions, innerHTML, and code loading order on browsers.


    On that at least, we can agree.
    Far from it, you seem to be overly preoccupied with yourself being right all the time irrespective of the fact that in reality their is no right or wrong way to produce code, you can write code that is untidy that does the same job in fewer blocks of code or take the standards approach because someone else says that how you should do it. Talk about stifling inventiveness.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles