THESE FORUMS NOW CLOSED (read only)

  • 16 Apr 2024, 10:21
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]   Go Down

Author Topic: Need help (lots) with Javascript  (Read 11009 times)

bryntheskits

  • Larger than most fish
  • **
  • Offline Offline
  • Posts: 114
Re: Need help (lots) with Javascript
« Reply #50 on: 27 Aug 2013, 11:49 »

Ah ok.  Gonna keep the ;'s then, since I'm still getting used to the code.

Ok, here's another thing I've run into, and it took some hair pulling and whatnot to figure out.  Here's code for two buttons:

Code: [Select]
<button onclick="timedCount()">Start count</button>

<input type="button" value="Start count" onClick="timedCount()" />
Both point to the same function when placed in the same block of code, but for some reason, only the top one works (this is for a basic timer; text of the function is the same for both attempts).  The second one creates a button, but its nonfunctioning.  I've had this happen several other times, and each time when I went back and changed the code from the second format to the first's, then it works fine.  What causes one to work and the other to not?

Get rid of the / after the onclick code, don't need that.

Other than that I cannot see what the problem could be, are you using anything to debug the Javascript? Even Chrome comes with a developer console, very handy.

EDIT: Nevermind, found the problem (what I could replicate here, anyway); onclick is supposed to be all lowercase, since methods are case sensitive it will only work that way, the 2nd bit of code you have there is in camel case (onClick) when it should be lower, like the 1st bit of code (onclick).
« Last Edit: 27 Aug 2013, 11:54 by bryntheskits »
Logged
Hurp

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: Need help (lots) with Javascript
« Reply #51 on: 27 Aug 2013, 12:37 »

So, I wrote some test code:

Code: [Select]
<html>
<button onclick="timedCount(this)">Start count</button>
<input type="button" value="Start count" onClick="timedCount(this)" />
<script>
function timedCount(btn) {
console.log("pressed from: " + btn);
}
</script>
</html>

The "this" inside the onclicks just sends the button element in, so the timedCount method can print it out.
Pressing the first button results in: "pressed from: [object HTMLButtonElement] "
Pressing the second button results in: "pressed from: [object HTMLInputElement] "

So it seems like both input and button works from my side. Above poster is correct in that the onclick should be all lower case, but chrome handles both onclick and onClick in both the button and the input. Not sure about other browsers. You are wrong on the / not being required, it closes off the input tag... but it doesn't seem to matter if it's there or not. Javascript :psyduck:

Sorflakne, can you post your code? It'd help to be able to look at it.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

pwhodges

  • Admin emeritus
  • Awakened
  • *
  • Offline Offline
  • Posts: 17,241
  • I'll only say this once...
    • My home page
Re: Need help (lots) with Javascript
« Reply #52 on: 27 Aug 2013, 13:30 »

Get rid of the / after the onclick code, don't need that.
You are wrong on the / not being required, it closes off the input tag... but it doesn't seem to matter if it's there or not.

<input> has no end tag in HTML (any version); however, in XHTML, the end tag is required.
Logged
"Being human, having your health; that's what's important."  (from: Magical Shopping Arcade Abenobashi )
"As long as we're all living, and as long as we're all having fun, that should do it, right?"  (from: The Eccentric Family )

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: Need help (lots) with Javascript
« Reply #53 on: 27 Aug 2013, 14:22 »

Erp, didn't spot that, you are right.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

bryntheskits

  • Larger than most fish
  • **
  • Offline Offline
  • Posts: 114
Re: Need help (lots) with Javascript
« Reply #54 on: 27 Aug 2013, 21:41 »

but chrome handles both onclick and onClick in both the button and the input.
Strange, for lack of proper time to test I used the developer console to change onclick to onClick and the onClick and it's variable disappears.
Logged
Hurp

Sorflakne

  • Duck attack survivor
  • *****
  • Offline Offline
  • Posts: 1,721
Re: Need help (lots) with Javascript
« Reply #55 on: 28 Aug 2013, 13:25 »

Quote
Sorflakne, can you post your code? It'd help to be able to look at it.
I'm trying to remember which one it was (I've made several...serves me right for not giving each a unique name), but if I do, I'll post it.

On the topic of ASP stuff, the book I'm using has its ASP examples "written in VBScript for an Internet Information Server", to quote from it, and then has the file saved with an .asp extension.  Why do this, if the file is saved as .asp?  I get that for me this is rather advanced stuff so maybe I'm not seeing the reasoning for doing this yet (or maybe the book is wrong and you're not supposed to do this), but why not write the files in ASP and save them as such?  And on a similar subject, between ASP and PHP, which is the preferred coding, or are there times where one or the other is specifically required?

Quote
Other than that I cannot see what the problem could be, are you using anything to debug the Javascript? Even Chrome comes with a developer console, very handy.
I'm going to the console in Firefox and selecting Debug Mode, but nothing happens.  It displays the page code, but there's no indicators for stuff that's wrong, no way to manipulate text or whatnot...not really sure how to use it.
« Last Edit: 28 Aug 2013, 13:36 by Sorflakne »
Logged
If you want to see what God and Satan look like, look in the mirror.

bryntheskits

  • Larger than most fish
  • **
  • Offline Offline
  • Posts: 114
Re: Need help (lots) with Javascript
« Reply #56 on: 29 Aug 2013, 03:49 »

ASP is more Windows based servers, while PHP is any (that's how I see it). I prefer PHP much more, and interact with more things and well I grew up with it so maybe I'm bias.

I don't use Firefox but there should be a console somewhere in the Developers part, there is in Chrome anyway.

https://developer.mozilla.org/en-US/docs/Tools/Web_Console

You should also be able to change code on the fly using the Inspecter (I think :P)
Logged
Hurp

Loki

  • comeback tour!
  • *****
  • Offline Offline
  • Posts: 5,532
  • The mischief that dwells within
Re: Need help (lots) with Javascript
« Reply #57 on: 29 Aug 2013, 04:19 »

This is just a guess, but:

A button can stand alone pretty much anywhere in the document [1]. An input MUST be within a form tag to confirm to the HTML standard, so that may be the problem.

Pretty sure there is no standard on attribute lower/upper case in the HTML specification. XHTML requires all lowercase. Are you using any document type declaration? If not, you should probably choose one (I think nowadays it doesn't really matter if you choose HTML5 or XHTML.) The important thing is that you stick to it. Make sure your HTML code uses the right syntax using http://validator.w3.org/.

The DTD basically tells the browser - any properly-programmed browser "hey, I am doing things this way". If you do things wrong, the browser will try to correct your code to what it thinks it should be, but it may be wrong.

No idea about ASP at all, sorry. However note that you can configure your server to treat any extension as anything you want, it is just not a good idea to do so. (You can also configure your PC to open all mp3-files with an image viewer, but that won't be a good idea either).

You might want to use http://www.jslint.com/ for validating the Javascript syntax. You will probably want to check at least the following:

Assume:
a browser,
console, alert,..

Tolerate...
assignment expressions
bitwise operators
continue
== and !=
++ and --
unused parameters

everything in the fourth column.
Logged
The future is a weird place and you never know where it will take you.
the careful illusion of shit-togetherness

Sorflakne

  • Duck attack survivor
  • *****
  • Offline Offline
  • Posts: 1,721
Re: Need help (lots) with Javascript
« Reply #58 on: 29 Aug 2013, 11:13 »

Eh, most of the examples I've seen don't have <form> tags around the buttons if they're standalone.  Now if the buttons are connected with a text box or are otherwise more than just the button itself, then yeah, they pretty much all have <form> tags.

So between PHP and ASP, is it better to get smart on one and have at least a basic understanding of the other, or should a person know both about equally?

Also gave JSLint a try, and it kept telling me that I was missing "use strict" statements all over my code.  I've never heard of this, is "use strict" required for javascript writing?  Some of the info I've found says you can just put it in the first line of your javascript page, assuming you have all your stuff saved to a separate file from the webpage itself, and others say you can simply write it into its own function...how would you write a function for it?

On the other hand, JSLint seems nice, but it keeps giving back "so-and-so has not been defined"; it even does this for alert(); lines...how do you define alert(); (yes, there is messages enclosed by "" in them :P )?  I mean, the code I'm writing is modified from the book I'm using and trying to follow a similar format to it, which works fine when directly copied and tested in the browser, but JSLint keeps coming back with undefined's for the book's stuff and my own, even when adding exceptions in the Tolerate options.
« Last Edit: 29 Aug 2013, 11:38 by Sorflakne »
Logged
If you want to see what God and Satan look like, look in the mirror.

Loki

  • comeback tour!
  • *****
  • Offline Offline
  • Posts: 5,532
  • The mischief that dwells within
Re: Need help (lots) with Javascript
« Reply #59 on: 29 Aug 2013, 11:29 »

*shrug*  I know my PHP and not a hint of ASP, served me well enough so far. (Of course, I don't program that much anymore. I study Computer Sciences after all.)
Logged
The future is a weird place and you never know where it will take you.
the careful illusion of shit-togetherness

Sorflakne

  • Duck attack survivor
  • *****
  • Offline Offline
  • Posts: 1,721
Re: Need help (lots) with Javascript
« Reply #60 on: 29 Aug 2013, 11:36 »

But ASP looks so much cleaner than PHP though :P (sucks that it's more Windows-oriented than universal in that regard...)
Logged
If you want to see what God and Satan look like, look in the mirror.

Loki

  • comeback tour!
  • *****
  • Offline Offline
  • Posts: 5,532
  • The mischief that dwells within
Re: Need help (lots) with Javascript
« Reply #61 on: 29 Aug 2013, 11:43 »

Then learn ASP?

Out of curiosity, are you learning a serverside language for any special reason or just for the fun of it?
Logged
The future is a weird place and you never know where it will take you.
the careful illusion of shit-togetherness

pwhodges

  • Admin emeritus
  • Awakened
  • *
  • Offline Offline
  • Posts: 17,241
  • I'll only say this once...
    • My home page
Re: Need help (lots) with Javascript
« Reply #62 on: 29 Aug 2013, 11:44 »

There comes a point, eventually (trust me), when you "get" the concepts that vary between languages in such a way that you can see them all as restricted dialects of one greater "computer language".  You may still have to learn different method names and the like in different environments, but that's what manuals are for, right?

Well, your mileage may vary I suppose, but that's how it went for me.

But as for ASP - that's a specific MS technology (though there is an open-source version), and most likely to be encountered in commercial all-Windows environments using IIS.  And even then something else, like PHP or Java/JSP or something is as common.  I wouldn't suggest learning ASP until you have an identified need to.  I currently have sites running PHP, Java/JSP and Perl on my Windows Server at home, but no ASP; in the office it's PHP, Java/JSP, Perl and ASP.
Logged
"Being human, having your health; that's what's important."  (from: Magical Shopping Arcade Abenobashi )
"As long as we're all living, and as long as we're all having fun, that should do it, right?"  (from: The Eccentric Family )

Sorflakne

  • Duck attack survivor
  • *****
  • Offline Offline
  • Posts: 1,721
Re: Need help (lots) with Javascript
« Reply #63 on: 29 Aug 2013, 11:59 »

Yeah, I'll probably wait until I'm more comfortable with javascript before diving too deeply into them, but it's nice to have an idea of them.

@Loki: I would like to put it to use eventually doing web design.  Yeah, I'm aware I've a ways to go in that regard.
Logged
If you want to see what God and Satan look like, look in the mirror.

bryntheskits

  • Larger than most fish
  • **
  • Offline Offline
  • Posts: 114
Re: Need help (lots) with Javascript
« Reply #64 on: 30 Aug 2013, 09:04 »

There comes a point, eventually (trust me), when you "get" the concepts that vary between languages in such a way that you can see them all as restricted dialects of one greater "computer language".
^this

All through my CS degree this has been drilled into me, and it is very true for a lot of languages.
Logged
Hurp
Pages: 1 [2]   Go Up