|
geeves posted:I don't know if this is a jQuery thing or not, but has anyone every successfully made a variable inside an anonymous function readable outside of it? Just assign that variable to some arbitrary property on "window". The window object is also the top level scope, so any properties you define on it are global variables. Simple example: code:
quote:
code:
code:
code:
Also, no need to pass $ and window to the function, as your function does nothing with its arguments. epswing posted:Can I ask why you want to do this?
|
# ? Oct 9, 2010 22:01 |
|
|
# ? May 17, 2024 02:34 |
|
Xenogenesis posted:Just assign that variable to some arbitrary property on "window". The window object is also the top level scope, so any properties you define on it are global variables. Simple example: That was it, thanks. It has been a couple of months since I've seen the code and I didn't write the part that started with ($, window, function) ... so I don't know why that was in there and I had too many other things to work on that I never got around to doing a code review before I left my old job.
|
# ? Oct 9, 2010 22:10 |
|
I need a visualization/graphing JS library. Any suggestions?
|
# ? Oct 19, 2010 16:15 |
|
wins32767 posted:I need a visualization/graphing JS library. Any suggestions? Flot: http://code.google.com/p/flot/
|
# ? Oct 19, 2010 19:00 |
|
Is JavaScript absolutely necessary for the whole thing? If not, you can integrate Open Flash Chart through JSON/Flash' standard methods to work with your JavaScript. processing.js is also an option.
|
# ? Oct 19, 2010 19:42 |
|
i am not sure if this is a Js question or html. I have a text box that a user will be typing a currency value into it. I would like a comma to be inserted while the user is typing if they go over a a thousand, and a period where decimals are applicable. Here is an example of the input box so far. <input type="text" value="0.0"onblur="if (this.value=='') this.value='0.0';" onfocus="if (this.value==this.defaultValue) this.value='';" name="YearMisc" />
|
# ? Oct 19, 2010 21:36 |
|
I'm a complete noob with Javascript and web-programming in general, bah. So I have a calendar for which I want to create a quick-jump with drop down menus. The url for a given month would, for example, look like this:code:
code:
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? AshB fucked around with this message at 23:51 on Oct 19, 2010 |
# ? Oct 19, 2010 23:41 |
|
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:
|
# ? Oct 20, 2010 00:29 |
|
DholmbladRU posted:i am not sure if this is a Js question or html. It's a javascript question. You want to attach a keyup event to the input, and every time they enter something, pass the value through a regex that removes all non-digits, then adds commas in the right places, and sets the form input to that new string. Tivac posted:
code:
Lumpy fucked around with this message at 01:11 on Oct 20, 2010 |
# ? Oct 20, 2010 01:09 |
|
wins32767 posted:I need a visualization/graphing JS library. Any suggestions? ProtoVis?
|
# ? Oct 20, 2010 02:38 |
|
Lumpy posted:It's a javascript question. You want to attach a keyup event to the input, and every time they enter something, pass the value through a regex that removes all non-digits, then adds commas in the right places, and sets the form input to that new string. I would use the onkeyup correct? Im looking through tutorials on regexpressions now, I do not have much experience with them or web programing.
|
# ? Oct 20, 2010 02:52 |
|
DholmbladRU posted:I would use the onkeyup correct? Im looking through tutorials on regexpressions now, I do not have much experience with them or web programing. Yeah, using jQuery because I'm lazy, you'd do something along the lines of: code:
|
# ? Oct 20, 2010 05:11 |
|
Lumpy posted:
It makes me wonder why they don't simply build jQuery syntax into the next version of ECMAScript.
|
# ? Oct 20, 2010 14:25 |
|
Golbez posted:It makes me wonder why they don't simply build jQuery syntax into the next version of ECMAScript. That's like asking why don't they just change the language to Scheme and call it done.
|
# ? Oct 20, 2010 19:09 |
|
Although, now that I look at it, that works? You get the .val() of a select? I've been doing, $('#my_select option:selected').val().
|
# ? Oct 20, 2010 20:25 |
|
Golbez posted:Although, now that I look at it, that works? You get the .val() of a select? I've been doing, $('#my_select option:selected').val(). Yup, it's worked since at least 1.3.x, probably earlier.
|
# ? Oct 20, 2010 20:58 |
|
Golbez posted:It makes me wonder why they don't simply build jQuery syntax into the next version of ECMAScript. What does this mean? It SOUNDS like you're talking about document.querySelector, but thats the DOM, and most desktop browsers (except IE8 and below) support it, but maybe you mean something else?
|
# ? Oct 21, 2010 03:55 |
|
Xenogenesis posted:What does this mean? Then perhaps swap 'want ECMAScript to have the syntax' to 'burn all the books and websites that told Tivac to do it that way'. Wasn't aware of querySelector, and I blame said references for that too.
|
# ? Oct 21, 2010 07:04 |
|
querySelector is bloody marvellous, and even IE8 supports it.
|
# ? Oct 21, 2010 11:34 |
|
Can this be done? var global_info_a = 0; var global_info_b = 0; blah (10, global_info_a); blah (20, global_info_b); function blah(stuff, info) { * magic voodoo so global_info_a=10 and global_info_b=20 * }
|
# ? Oct 29, 2010 06:05 |
|
Schweinhund posted:Can this be done? Nope. Maybe something like: code:
|
# ? Oct 29, 2010 06:16 |
|
You can use closure to bind to those variables...code:
epswing fucked around with this message at 06:23 on Oct 29, 2010 |
# ? Oct 29, 2010 06:20 |
|
FeloniousDrunk posted:Nope. Maybe something like: if you're in a browser, then window is the global object, and you can access the variables as properties of it. code:
peepsalot fucked around with this message at 07:04 on Oct 29, 2010 |
# ? Oct 29, 2010 07:02 |
|
Thanks. It will be easier to just write separate blaha and blahb functions for now, but I'll keep those options in mind in the future.
|
# ? Oct 29, 2010 07:28 |
|
*cringes having to bump this thread* I'm in an intro to web development class so I'm new to this stuff. I'm stuck on one small thing in this assignment and I was hoping someone could nudge me in the right direction. I have a select box that lists different colors. I need it to be able to change the background color of the page, to the color that the person has selected. I was able to get it to work with this: code:
code:
code:
The rest of the webpage works fine so I know the .js file is being read properly (it's an image gallery). What am I missing here?
|
# ? Nov 7, 2010 19:10 |
|
Untested, but I would try something likecode:
code:
|
# ? Nov 7, 2010 19:40 |
|
Thanks for the reply! It was a good idea but it didn't work I made a blank page just to test this (in case it was something else interfering) but it didn't make a difference.
|
# ? Nov 7, 2010 20:19 |
|
It's good have a thorough understand of how the "this" keyword works. It will save you some headaches: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/this_Operator When you use the onchange or onwhatever attribute in an html element, it's basically like wrapping your attribute string in an anonymous function declaration. So your initial onchange"bgColor()" example is equivalent to assigning the following handler to your event: code:
If you read the link above, and notice that bgColor is called directly here, the context given to it(what "this" points to inside bgColor) is the global object(window). This doesn't work because you want this to point to the element that the event fired from. Accesing "this" from inside this anonymous function will get you the element you want, and that's why epswing's example should work, but it's still not really ideal. If you really want to separate content from behavior, you shouldn't be using those on____ html attributes at all. You should register your event handlers inside your script. This must be done after the DOM tree has been populated(either by including your script file at the end of the page, or by wrapping your event registrations inside the window.load event). Otherwise the elements won't exist for you to register events on. Here's a slightly better way to do what you want: code:
Assigning functions to the on____ properties on elements is a quick and dirty way of registering events, which is better than using the html attributes. It's dirty though because it does not support the registering of multiple events on any given object. Subsequent event registrations will override existing ones. Ideally you would use the W3C standard element.addEventListener method to register your events. But then there's the issue of IE not suppotring the standard and having their own attachEvent method, which complicates things. So you have to do some feature detection and branching depending on what the browser supports. This is why I prefer to use a library such as jQuery as it abstracts away some of the more tedious aspects of event regsistration and cross browser comaptibility in general. jQuery example: code:
peepsalot fucked around with this message at 20:31 on Nov 7, 2010 |
# ? Nov 7, 2010 20:28 |
|
BlackRider posted:Thanks for the reply! It was a good idea but it didn't work Hmm, I tested this since I found it hard to believe that epswing's example would not work, but sure enough I got an error "bgColor is not a function". Giving your functions the same name as html attributes can cause problems. bgcolor is an html element attribute. renaming it to setBgColor works fine: http://jsbin.com/eheru3/2/edit peepsalot fucked around with this message at 20:52 on Nov 7, 2010 |
# ? Nov 7, 2010 20:46 |
|
Awesome. Thank you peepsalot for taking the time to explain all of that to me. I had to re-read it a few times to really understand it but it was very thorough and well explained. If you ever get sick of whatever it is you do, maybe you should teach this stuff. Can't thank you enough, I wasn't expecting that kind of response. jQuery is very intriguing to me and I didn't know it existed. Part of the problem with learning some of this stuff is everything we are doing is pretty antiquated. I think I will get familiar with this on my own. As far as the function name being the same as an element attribute
|
# ? Nov 7, 2010 21:12 |
|
peepsalot posted:Hmm, I tested this since I found it hard to believe that epswing's example would not work, but sure enough I got an error "bgColor is not a function". Giving your functions the same name as html attributes can cause problems. bgcolor is an html element attribute. whoops
|
# ? Nov 7, 2010 22:06 |
|
I'm developing a JavaScript game and I quickly ran into issues trying to do inheritance. Google was no help so I sat down and created a prototypal inheritance pattern. I wrote an article about it and I'm looking for feedback -- what do you guys think? http://www.iokat.com/posts/2/a-javascript-prototypal-inheritance-pattern-that-doesnt-suck
|
# ? Nov 13, 2010 19:40 |
|
Gordon Cole posted:I'm developing a JavaScript game and I quickly ran into issues trying to do inheritance. Google was no help so I sat down and created a prototypal inheritance pattern. I wrote an article about it and I'm looking for feedback -- what do you guys think? One nit pick off the bat: your atricle posted:The current solutions to implementing JavaScript inheritance tend to be clunky and impractical. If you are going to say this, back it up. I know my js inheritance pattern certainly isn't clunky or impractical. In a "serious" article, using that type of language can set the wrong tone to your audience. As for the article... it's decent, but there are tons like it out there. The chapter on Inheritance form Crockford's java script: The Good Parts touches on all this as well, and your pattern here seems like a variation on the Functional inheritance he describes there. That said, it is well written, clear, and I wish I was as good at writing about code as you are.
|
# ? Nov 13, 2010 22:16 |
|
Thanks for the advice Perhaps I should take that part out. The reason I developed my own pattern to begin with was that everything I found on Google kind of sucked. I mostly found patterns that tried to recreate class-based object orientation or ones that didn't support extending functions. But Googling now I'm finding some pretty decent solutions, so I guess I wasn't using very good search terms before. Which is probably true because I was still learning the more advanced parts of JavaScript while I was coming up with this.
|
# ? Nov 14, 2010 11:30 |
|
I'm currently working on a Google Chrome extension, but I'm a Javascript newbie and I'm running into a problem I can't figure out. I'm using XMLHttpRequest to grab a web page that I want to parse. The Chrome extension tutorials give this example: code:
code:
|
# ? Nov 14, 2010 23:42 |
|
almostkorean posted:I'm currently working on a Google Chrome extension, but I'm a Javascript newbie and I'm running into a problem I can't figure out. Not knowing specifics, my guess is because HTML is not JSON. JSON.parse is likely expected to act on "stringified" JSON. I don't know what JSON lib you are using, but the one at json.org which is the most commonly used one says this about the parse() method: JSON docs posted:The parse method takes a text and an optional reviver function, and returns So it seems like you are taking HTML and attempting to convert it to JSON, and it (correctly) failing.
|
# ? Nov 15, 2010 00:04 |
|
Gordon Cole posted:http://www.iokat.com/posts/2/a-javascript-prototypal-inheritance-pattern-that-doesnt-suck You mention Firefox in the article, but even the latest 3.x doesn't seem to support Object.create() so you might want to explicitly mention which browsers (or just which JS engines) it's going to work on if it's that badly supported.
|
# ? Nov 15, 2010 22:52 |
|
Lumpy posted:Not knowing specifics, my guess is because HTML is not JSON. JSON.parse is likely expected to act on "stringified" JSON. I don't know what JSON lib you are using, but the one at json.org which is the most commonly used one says this about the parse() method: Thanks, you are right . With that said, I'm 90% sure I'm going to finish making this extension using GWT. I really can't stand Javascript
|
# ? Nov 16, 2010 00:52 |
|
almostkorean posted:Thanks, you are right . With that said, I'm 90% sure I'm going to finish making this extension using GWT. I really can't stand Javascript
|
# ? Nov 16, 2010 02:50 |
|
|
# ? May 17, 2024 02:34 |
|
Nigglypuff posted:Care to elaborate? People who don't know javascript and try to write it w/o learning it hate javascript. Film at 11.
|
# ? Nov 16, 2010 03:09 |