javascript

JavaScript: given a list of image URLs, how would one display the first non-broken image?

I receive a list of image URLs and currently I have several hidden img elements on my page (one for each URL in the list). When the page has finished loading, I use JavaScript to check the images and display (i.e., set myImage.style.display="inline") the first one that is not broken. This is quite simple. However, it requires that I requ...

document.write() vs inserting DOM nodes: preserve form information?

Consider two web pages with the following in their body respectively: <body> <script> document.writeln('<textarea></textarea>') </script> </body> and <body> <script> var t = document.createElement('textarea'); document.body.appendChild(t); </script> </body> (think of them as part of something larger, where the textareas have to be ...

javascript - associative array without toString, etc...

I want to create an associative array: var aa = {} //equivalent to Object(), new Object(), etc... and I want to be sure that any key I access is going to be a number: aa['hey'] = 4.3; aa['btar'] = 43.1; I know JS doesn't have typing, so I can't automatically check this, but I can ensure in my own code that I only assign strings to ...

Testing REST web service client when the services don't yet exist

I have already read the posts link text and link text but these are covering the use case of building a REST service on the server and requiring a rapid client in order to test those services. We have the opposite problem, we are building a web client that will communicate to the server via REST, but the decision has been taken to let t...

How do I fix: InvalidOperationException upon Session timeout in Ajax WebService call

Hi All, We are invoking Asp.Net ajax web service from the client side. So the JavaScript functions have calls like: // The function to alter the server side state object and set the selected node for the case tree. function JSMethod(caseId, url) { Sample.XYZ.Method(param1, param2, OnMethodReturn); } function OnMethodReturn(re...

Trouble adding onclick handler to hyperlink tag inside listitem

Trying to add an onclick handler to my tabs, and can't seem to get the DOM selection right. Can you guys help? <div id="tabstrip"> <ul> <li id="a" class="selected"><a href="#">A</a></li> <li id="b"><a href="#">B</a></li> <li id="b"><a href="#">C</a></li> </ul> </div> function initTabStrip() { var lis = d...

JavaScript - detect if function called as constructor

Given a function: function x(arg) { return 30; } You can call it two ways: result = x(4); result = new x(4); The first returns 30, the second returns an object. How can you detect which way the function was called inside the function itself? Whatever your solution is, it must work with the following invocation as well: var Z = n...

Prevent caching of AJAX call

It looks like that if I load dynamic content using $.get(), the result is cached in browser. Adding some random string in QueryString seem to solve this issue (I use new Date().toString()), but this feels like a hack. Is there any other way to achieve this? Or, if unique string is the only way to achieve this, any suggestions other tha...

javascript function not behaving correctly

hello, I have this little function function makewindows(){ child1 = window.open ("about:blank"); child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>"); child1.document.close(); } Which whatever I try, simply outputs the php code as the html source, and not the result of the php code...

Javascript - onchange within <option>

Hi, I have a relatively simple form which asks a variety of questions. One of those questions is answered via a Select Box. What I would like to do is if the person selects a particular option, they are prompted for more information. With the help of a few online tutorials, I've managed to get the Javascript to display a hidden div jus...

JavaScript Hashmap Equivalent

As made clear in update 3 on this answer, this notation: var hash = {}; hash[X] does not actually hash the object X; it actually just converts X to a string (via .toString() if it's an object, or some other built-in conversions for various primitive types) and then looks that string up, without hashing it, in "hash". Object equality i...

Best light weight JavaScript IDE

What is a good light weight JavaScript IDE? I don't care too much for the one built into VS because it seams lacking. I've been using 1st JavaScript Editor Pro as a alternative for working with JavaScript but I wanted to see if there is any thing else out there that is better and light weight. ...

Readonly SELECT tag

According to HTML specs, the select tag in HTML doesn't have a readonly attribute, only a disabled attribute. So if you want to keep the user from changing the dropdown, you have to use disabled. The only problem is that disabled HTML form inputs don't get included in the POST / GET data. What's the best way to emulate the readonly att...

setting focus to iframe contents.

I have a page with a document.onkeydown event handler, and I'm loading it inside an iframe in another page. I have to click inside the iframe to get the content page to start "listening". is there some way I can use javascript in the outer page to set the focus to the inner page so I don't have to click inside the iframe? EDIT: resspons...

javascript regex to extract anchor text and URL from anchor tags

Hello, I have a paragraph of text in a javascript variable called 'input_content' and that text contains multiple anchor tags/links. I would like to match all of the anchor tags and extract anchor text and URL, and put it into an array like (or similar to) this: Array ( [0] => Array ( [0] => <a href="http://yahoo...

Why should you not use Number as a constructor?

I entered this statement in JSLint: var number = new Number(3); And received the following message: Do not use Number as a constructor. Why is that? The statement is creating a number object, not a primitive value, so I don't see why using new is a problem. EDIT: Thanks for all the responses. They've got me thinking further, so...

Question about object.method in JavaScript

This is a follow-up question to this one. Take a look at these two examples: var number1 = new Number(3.123); number1 = number1.toFixed(2); alert(number1); var number2 = 3.123; number2 = number2.toFixed(2); alert(number2); I realize they both end up with the same value, but is it correct thinking to refer to a method of a primitiv...

How to prevent IFRAME from redirecting top-level window

Some websites have code to "break out" of IFRAME enclosures, meaning that if a page A is loaded as an IFRAME inside an parent page P some Javascript in A redirects the outer window to A. Typically this Javascript looks something like this: <script type="text/javascript"> if (top.location.href != self.location.href) top.location....

PageMethods with ASP.Net MVC

I have found ASP.Net PageMethods very handy and easy to use, but I have just started developing using MVC and am not sure how to use them? What is the equivalent of PageMethods.MyFunction() in MVC where MyFunction is a Controller action? I know I can use the Json function to return a value, but how do I call the action from the client...

How can I include css files from an MVC partial control?

I'm using ASP.NET MVC and I have a partial control that needs a particular CSS & JS file included. Is there a way to make the parent page render the script and link tags in the 'head' section of the page, rather than just rendering them inline in the partial contol? To clarify the control that I want to include the files from is being ...