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
There Will Be Penalty
May 18, 2002

Makes a great pet!
JSON would be better, but any browser's HTML DOM methods will also work with responseXML.

Quickie Google search yields: http://www.w3schools.com/Ajax/ajax_responsexml.asp

which should serve as a very basic example.

EDIT: prototype.js may have some methods as well. I've no experience with prototype myself; I'm extrapolating that based on my experience with jQuery, which has some nice XPath-ish methods.

There Will Be Penalty fucked around with this message at 01:31 on Feb 3, 2009

Adbot
ADBOT LOVES YOU

There Will Be Penalty
May 18, 2002

Makes a great pet!

Packet Loss posted:

Just the tags - I have a table that I can't access the HTML on, I need to remove just the row tags, so I can have just columns.

The W3C HTML standards say you can't just stick a <td> directly inside a <table>.
You need a single <tr> to contain all the columns.

table.getElementsByTagName("tr") will yield a list of table rows inside the given table.

Iterate through each one to get their innerHTML.

Concatenate all those values and stick them inside a single <tr> and you will probably be OK.

However this is all moot...

Packet Loss posted:

The table has an ID, but evidently there's an underscore in the ID which I believe is invalid and so my code always fails.

Underscores are perfectly fine in an ID: http://www.w3.org/TR/html4/struct/global.html#h-7.5.2

So the problem is something else.

There Will Be Penalty
May 18, 2002

Makes a great pet!

heeen posted:

I read somewhere, that JSON is significantly faster to parse on the client than xml.

And the code to access data inside JSON is much shorter.

And JSON explicitly distinguishes between arrays and hashes.

And you don't have that problem with some XML-to-JavaScript-object parsers where <foo><bar>1</bar><bar>2</bar></foo> and <foo><bar>1</bar></foo> result in different types of JavaScript object structures.

There Will Be Penalty fucked around with this message at 23:06 on Feb 4, 2009

There Will Be Penalty
May 18, 2002

Makes a great pet!

Arf posted:

code:
var pos = 270
function Scroll() {
    obj = document.getElementById('scrollerdiv');
    pos -= 1;
    obj.style.top=pos;
    window.setTimeout("Scroll();",50);
}
window.onLoad = Scroll();

I see these things wrong right off the bat:

1. window.onload, like everything else in JavaScript, is case-sensitive.

2. window.onload needs to refer to the function itself:

window.onload = Scroll;

Instead, you're calling it and assigning its return value to window.onload.

3. Don't use numbers alone when setting a CSS property that takes a dimension:

obj.style.top = pos + "px";

4. Not strictly wrong, but passing a string as the first argument to window.setTimeout or window.setInterval is bad form, and almost never necessary. Again, you can just pass a reference to the function (don't call the function) instead:

window.setTimeout(Scroll, 50);

5. You are not terminating all of your statements with semicolons. This will cause you problems in the future.

There Will Be Penalty
May 18, 2002

Makes a great pet!

Arf posted:

Thanks for your help, turns out it was the pixel thing. Also taken on board your comments about form. Don't worry, this is not going anywhere near anything important.

No problem.

But it was not just the pixel thing. window.onLoad should *not* have been working. The last line of your code may have happened to work, but that is only because certain things were in a certain order.

There Will Be Penalty
May 18, 2002

Makes a great pet!

Blackout posted:

Hey guys, quick question. I'm trying to write a very simple piece of javascript that does two things. First, it will either show or hide an HTML div with the first id I specify. Second, it will change the text of the div with the second id specified. It should toggle these things, so if the div is shown, it will be hidden and vice versa. Same thing with the text (switches between 'Show...' and 'Hide').

Your JavaScript appears to be up to snuff. I'd have to see some generated HTML, incase there's any funky stuff going on.

There Will Be Penalty
May 18, 2002

Makes a great pet!

Tivac posted:

Arrays can only have numeric indexes, hence why you can't use ['menus']['buckets'].

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

That explanation contradicts you. :)

Crockford posted:

Arrays in JavaScript are also hashtable objects.

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.

There Will Be Penalty fucked around with this message at 02:31 on Apr 22, 2009

There Will Be Penalty
May 18, 2002

Makes a great pet!
One other thing I noticed about JavaScript.

The equivalence of the dot and bracket notations for accessing object properties extends to method calls. This includes the aspect that the value of this during the method call is set to the referenced object. Meaning: o.method(args) and o["method"](args) are exactly equivalent.

This means you can write code such as the following:

code:
var visible;                    /* a boolean */
/* ... */
$("#div0")[visible ? "show" : "hide"]();
This is equivalent to calling either $("#div0").show() or $("#div0").hide() depending on visible's value.

EDIT: I know that in most instances you can use $("#div0").toggle(). This is an example. But sometimes you may wish to maintain the state separately.

There Will Be Penalty fucked around with this message at 02:44 on Apr 22, 2009

There Will Be Penalty
May 18, 2002

Makes a great pet!

NotShadowStar posted:

Good try, but they mostly just copy/pasted the Mozilla ones which are :wtf:. Var declarations all over scopes, bit shift operations,

The bit-shift operators are most likely being used as a form of type coercion, specifically into a numeric type. I would have just used "foo + 0" myself, but there might be something going on of which I know nothing.

Comments would have been nice.

They probably copied the MDC code for the reason below (emphasis mine) and simply didn't want to mess with code that worked:

Mozilla posted:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Filter
filter is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of filter in ECMA-262 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object and TypeError have their original values, that fun.call evaluates to the original value of Function.prototype.call, and that Array.prototype.push has its original value.

Above specifically refers to Array.prototype.filter, but the docs for other Array methods on MDC have similar statements.

There Will Be Penalty
May 18, 2002

Makes a great pet!

NotShadowStar posted:

Lua and Ruby the semi-colon is defined as optional. ECMAScript the semicolon is not-optional, but the interpreter tries to fix this for you, and is usually wrong.

ECMA-262 posted:

7.9 Automatic Semicolon Insertion

There Will Be Penalty
May 18, 2002

Makes a great pet!
I believe it's safe to say that automatic semicolon insertion is invoked when it *can* be, i.e., when it won't cause a syntax error to do so at EOL.

It will not be invoked after an infix operator at EOL. It will also not be invoked if your innermost matching brackets are parenthesis, square brackets, or curly brackets used for object literal notation. I use these as rules of thumb about where it's safe to insert a newline (because gently caress a bunch of lines that are more than 80 columns long). So there's no reason to worry about any of the following statements:
code:
foo.buttz(
        balls +
        dooky
);
var buttz = {
        farts: "brrrt",
        balls: "cocks"
};
buttz.balls.cocks[
        i * i + j * j
] = 5;
It *will* be invoked if your innermost matching brackets are the curly brackets surrounding the body of a function, of course:
code:
foo.buttz(function () {
        x = 5           // it's invoked here
        + 6;
});
EDIT: it appears I was factually wrong as well. :bang:

There Will Be Penalty fucked around with this message at 06:43 on Jan 16, 2011

There Will Be Penalty
May 18, 2002

Makes a great pet!
It's innerHTML, not innerHtml.

Just about everything is case-sensitive here.

Also, this statement

code:
document.getElementById('inputfieldcellPHONE').style.display = 'none';
will *only* work if the <script> element it's in is somewhere after the <td> in question (like just before the closing </body> tag), or that statement is in an onload handler like what Munkeymon said.

EDIT: but when you start thinking about onload handlers and poo poo, I'd say gently caress it and use jQuery.

There Will Be Penalty
May 18, 2002

Makes a great pet!

Strong Sauce posted:

Does returning undefined cause an onsubmit to fail (e.g. not submit)?

No. You have to return false.

EDIT: returning undefined is the same as not returning a value at all. (All JavaScript functions return a value, really. If you don't specify one, they return undefined.)

There Will Be Penalty
May 18, 2002

Makes a great pet!

ddiddles posted:

So I'm trying to get a better understanding of the this keyword, and i ran into something that confuses me. It's probably because I dont understand JS objects fully, but this code:

code:
var testObject = {
  objectMethod : function() {
    console.log(this);
    
    function objectMethodFunction() {
      console.log(this);
    }
    
    objectMethodFunction();
  }

}

testObject.objectMethod();

Then I log the value of this with objectMethod, i get the object back, which I expected, but the objectMethodFunction inside of the method logs the window object, and I can't really understand why.

if you call a function as a method on an object, the `this` value is the object it's invoked on.
if you call a function as a function (without an object), the `this` value is either the global object (in non-strict mode) or `undefined` (in strict mode).
The value of `this` in the objectMethod call does not apply to inner nested functions like objectMethodFunction.

There Will Be Penalty
May 18, 2002

Makes a great pet!

Sab669 posted:

(Posting from my phone at work, sorry for poor formatting.)

TL:DRI'm looking for a JavaScript library to help iterate over HTML elements. Any suggestions?

I had to do something very similar to this in C# last week. I used HTML Agility Pack for this and it was fairly easy to use once I got the hang of it. For that project, I was iterating over a table and pulling out the InnerText; fairly simple. This time around it's a bit more complicated.


I've got a table within a div, within a table, within yet another table. So we're 3 tables deep. Thankfully (surprisingly) the div containing the table I care about has an ID so it's easy to find.

This final table has N rows and 4 columns. The number of rows is generated dynamically at run time to include a number of input controls to act as search parameters for the user.

I'm trying to basically combine the plain text in the "odd numbered" <TD> elements, then get the value of the control within the "even numbered" <TD> elements.

Now that I've typed it out it doesn't sound too hard but I can't figure out how to do it with just plain JS. I tried using document.GetElementById("myDivID"), then use getElementsByTagName to get the table, but this doesn't give me its child nodes (unsurprisingly). I tried passing "input" to that as well, but I'm getting far more controls in that collection than I can see on the page, which might be due to the third-party controls we use.

Use table_element.querySelector("> tbody > tr > td:nth-child(even)")

(I think that would be the correct selector, I know it would work with jQuery.)

There Will Be Penalty
May 18, 2002

Makes a great pet!

Thermopyle posted:

Ehh, "_" has a specific meaning in most languages either by convention or by syntax..."the language requires me to put a variable here but we do not have a use for it."

That information is useful to have.

Munkeymon posted:

I usually use whocares, dontcare or whatever but that last one isn't super clear, I guess

JSLint recommends that you use the identifier ignore.

This communicates the idea way better.

(I use JSHint these days, which doesn't have that convention.)

There Will Be Penalty
May 18, 2002

Makes a great pet!
lol if you don't always run your jQuery code in some form of var $ = jQuery;

Adbot
ADBOT LOVES YOU

There Will Be Penalty
May 18, 2002

Makes a great pet!
return false; is an almost-equivalent of event.preventDefault();. event.stopPropagation(); is a different beast.

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