Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Flobbster posted:

Awesome, that's exactly the kind of reference I was looking for (and it looks fairly recent which is probably why I never came across it before). Thanks!

Crockford's awesome do whatever he tells you.

Adbot
ADBOT LOVES YOU

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

MonkeyMaker posted:

OK, I have a JSON object with two sub-objects. How do I reference a sub-object by name, not index?

Here's the JSON:
...snip...

which validates as...valid...on jsonlint.com. I can access ['menus'] or ['menus'][0], but trying to get ['menus']['buckets'] fails. I have to be able to do this by name since it'll be client-updated as plain text.

Arrays (using the []) notation, and objects (using {}) are different. Objects can be accessed using the dot notation or as an associative array. Arrays can only have numeric indexes, hence why you can't use ['menus']['buckets'].

http://javascript.crockford.com/survey.html has a good explanation.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

There Will Be Penalty posted:

That explanation contradicts you. :)

Meaning: because arrays are objects, they can have string-indexed properties. Restating Crockford, the special thing about Array objects is their magical length property. And because of the equivalence of the bracket and dot notations, the length property can be accessed via a["length"], by the way.

It is not common to use other string-indexed properties on an array, but it is common to extend Array.prototype with methods, which are nothing more than string-indexed object (or prototype) properties whose values are functions.

Technically they're a hashtable, yes. I over-generalized a bit to help with the distinction between when you'd want to use arrays vs objects. Never mind that in JS an array is technically just a specialization of an object...

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

diadem posted:

Thank you so much. This bug was a huge pain.

For some reason, spaces weren't cutting it, but characters worked fine. A slightly modified version of your last suggestion fixed the issue.

You're just triggering a reflow as far as I can tell, here's a good article about browser reflow.

http://www.stubbornella.org/content/2009/03/27/reflows-repaints-css-performance-making-your-javascript-slow/

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Jreedy88 posted:

Yeah, block formatting messed my page design anyways so I ended up just doing a display="none" on the element in javascript after the page had loaded.

The answer to your disappeared question is to use "new Date()" instead of just "Date()", fyi.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

dancavallaro posted:

What is an example of a use case where it makes sense to use == over ===?

Maybe he just really likes type coercion?

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Kekekela posted:

parseInt(x) == parseInt(y)

You probably want bases on those calls to parseInt or else if the input is something like "08" you aren't going to get the answer you want.

Always specify a base for calls to parseInt, seriously.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Kekekela posted:

Thanks for this. Just substitute in "function call returning a specific type == same function called with different params" if you're really not understanding what that post was trying to convey. (I realize you probably couldn't and won't be bothered to read what I was replying to, but just in case)

I'm just making sure anybody reading those posts realizes what a nasty bug they can unleash by not using bases with parseInt. Not picking on you or anybody in particular.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

AshB posted:

Edit: Okay I got this to work by completely removing all the Javascript. Apparently I only needed html for this. But just for my info, what's the proper way of using Javascript to get the value of the selected item in a drop-down menu?

code:
var select = document.getElementById("my_select");

var item = select.options[select.selectedIndex].value;

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Douglas Crockford posted:

Google wants their Java to run anywhere, so they translate it into javascript.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

NotShadowStar posted:

That reminds me, is there some sort of command-line JSLint like YUICompressor? I use the Rhino-based YUI compressor as a part of a build system and it's really handy. It looks like there used to be but it doesn't exist anymore.

For another option, I wrote about how to do this using the Windows Scripting Host a while ago, the post is about running it as an editor tool but most of it is still applicable.

http://patcavit.com/2010/05/11/jslint-in-programmers-notepad-revisited/

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Boz0r posted:

I'm trying to learn javacsript by studying code and I'd like to know what the following line does:

code:
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.";
I don't know what the ? and : signs do.

It's a ternary conditional.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

smug forum rear end in a top hat posted:

YUI Doc looks pretty good to me. I don't use it but it was suggested in a book about Javascript Patterns I sorta read a few months ago.

YUI Doc is pretty good, definitely recommend you check out the Dana theme for it if you go that route.

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Adahn the nameless posted:

I'm interested in learning javascript from some ASP.NET stuff I want to do. Does anyone have a recommended introduction site or something along those lines?

http://eloquentjavascript.net/

Also seconding the recommendation for Crockford's book, just go into it knowing that he is VERY opinionated & you don't have to agree with everything he says.

Adbot
ADBOT LOVES YOU

Tivac
Feb 18, 2003

No matter how things may seem to change, never forget who you are

Small White Dragon posted:

Hey, the Google+ button has code like the following if you change the language:

code:
<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
  {lang: 'ja'} 
</script>
I have never seen syntax like that in a call to an external JS library. How does that even work, and how can I do something like that with my own JS functions?

Chances are good they parse the DOM, looking for the <script> with the right src attribute, & then read its innerHTML. You don't get access to the content between <script> that have a src natively, but you can kludge it a bit. Since it's JSON you can then JSON.parse(script.innerHTML) to get a useful config object out of it.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply