javascript

How can I give keyboard focus to a DIV and attach keyboard event handlers to it?

I am building an application where I want to be able to click a rectangle represented by a DIV, and then use the keyboard to move that DIV Rather than using an event listener for keyboard events at the document level, can I listen for keyboard events at the DIV level, perhaps by giving it keyboard focus? Here's a simplified sample to i...

How can I get the content of the file specified as the 'src' of a <script> tag?

If I have a script tag like this: <script id = "myscript" src = "http://www.example.com/script.js" type = "text/javascript"> </script> I would like to get the content of the "script.js" file. I'm thinking about something like document.getElementById("myscript").text but it doesn't work in this case. ...

Is there a better way to do optional function parameters in Javascript?

I've always handled optional parameters in Javascript like this: function myFunc(requiredArg, optionalArg){ optionalArg = optionalArg || 'defaultValue'; //do stuff } Is there a better way to do it? Are there any cases where using || like that is going to fail? ...

How can I format numbers as money in JavaScript?

I would like to format a price in JavaScript. Basically, I have a float variable, and I'd like to have a function that will receive that variable, and output: "$ 2,500.00" What's the best way to do this? EDIT: OK, since I haven't gotten any answers better than the code I implemented, plus my own answer has been voted down and I can't ...

Best way to determine whether a XML attribute exists in Flex

I have a XML response from an HTTPService call with the e4x result format. <?xml version="1.0" encoding="utf-8"?> <Validation Error="Invalid Username/Password Combination" /> I have tried: private function callback(event:ResultEvent):void { if(event.result..@Error) { // error attr present } else { // ...

How do eliminate the flicker effect on ajax call

I am encountering a problem: On an html page, when i click a certain control (a tab panel title) i make an ajax call. From Wicket (java code) i call a javascript function that "redraws" all the componenets on my page (this is like a reload of the page). Everytime i do this i get a flickering effect on the html (which, as i said, afte...

Check if option is selected with jQuery, if not select a default

Using jQuery, how do you check if there is an option selected in a select menu, and if not, assign one of the options as selected. (The select is generated with a maze of PHP functions in an app I just inherited, so this is a quick fix while I get my head around those :) ...

How do you write Valid XHTML 1.0 Strict code when you are using javascript to fill an element that requires a child?

I'm running my site through the W3C's validator trying to get it to validate as XHTML 1.0 Strict and I've gotten down to a particularly sticky (at least in my experience) validation error. I'm including certain badges from various services in the site that provide their own API and code for inclusion on an external site. These badges u...

How to avoid a postback in javascript?

I have an ASP.NET page which has a button it it. The button click launches a modal dialog box using javacript. Based on the value returned by the modal dialog box, i want to proceed with, or cancel the post back that happens. How do i do this? ...

Regular expression to match non-english characters?

What is the easiest way to match non-english characters in a Regex? I would like to match all words individually in an input string, but the language may not be English, so I will need to match things like ü, ö, ß, and ñ. Also, this is in javascript/jquery, so any solution will need to apply to that. ...

How to hide a Pocket Internet Explorer element?

How can I hide an element in html on Pocket Internet Explorer running on a Windows Mobile 6 device. The following code does not work even though it apears to be setting the values correctly. function ShowCollapseElement(sLinkId, sContentId) { var oLinkElement; var oContentElement; //Get the elements oLinkElement = document.getElem...

What is the easiest way to read/manipulate query string params using javascript?

The examples I've seen online seem much more complex than I expected (manually parsing &/?/= into pairs, using regular expressions, etc). We're using asp.net ajax (don't see anything in their client side reference) and would consider adding jQuery if it would really help. I would think there is a more elegant solution out there - so fa...

Jagged Button edges in Internet Explorer

How do you remove the jagged edges from a wide button in internet explorer? For example: ...

JavaScript highlight table cell on tab in field

Hi All, I have a website laid out in tables. (a long mortgage form) in each table cell is one HTML object. (text box, radio buttons, etc) What can I do so when each table cell is "tabbed" into it highlights the cell with a very light red (not to be obtrusive, but tell the user where they are)? ...

How to handle an ActiveX event in Javascript

This is somewhat of a follow-up to an answer here. I have a custom ActiveX control that is raising an event ("ReceiveMessage" with a "msg" parameter) that needs to be handled by Javascript in the web browser. Historically we've been able to use the following IE-only syntax to accomplish this on different projects: function MyControl::R...

How to get a reference to the currently focused form field in JavaScript?

I'm looking for a cross-browser method - I know IE has something (I've already forgotten what), and the way to do it in Mozilla may have to do with a focusNode thing I found, that seems related to getting text selections. Methods involving jQuery or another common JS library are fine by me. Thanks! ...

How to check if an object is an instance of a NodeList in IE?

Why is NodeList undefined in IE6/7? <form action="/" method="post" id="testform"> <input type="checkbox" name="foobar[]" value="1" id="" /> <input type="checkbox" name="foobar[]" value="2" id="" /> <input type="checkbox" name="foobar[]" value="3" id="" /> </form> <script type="text/javascript" charset="utf-8"> (function...

"Access is denied" error on accessing iframe document object

For posting AJAX forms in a form with many parameters, I am using a solution of creating an iframe, posting the form to it by POST, and then accessing the iframe's content. specifically, I am accessing the content like this: $("some_iframe_id").get(0).contentWindow.document I tested it and it worked. On some of the pages, I started...

How can I detect that the client is scrolled to the top or bottom of a webpage?

I'm looking for a cross-browser method of detecting that a client web browser is scrolled all the way to the bottom (or top) of the screen. Really, the top is fairly easy, as scrY = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop is zero if you're at the top. The problem is that scrY seems to ret...

How do I execute a page-defined JavaScript function from a Firefox extension?

Hi, I'm creating a Firefox extension for demo purposes. I to call a specific JavaScript function in the document from the extension. I wrote this in my HTML document (not inside extension, but a page that is loaded by Firefox): document.funcToBeCalled = function() { // function body }; Then, the extension will run this on some even...