Browsing Category: Javascript

MSIE: “The web page you are viewing is trying to close the window”

Monday, April 28th, 2008

The trials and tribulations of Internet Explorer can make the hardiest of us want to cry. When you code intranet applications, you often use new windows to simulate forms and whatnot. Yet MSIE7 loves to warn the user when the owner of the popup tries to close the very popup it opened!

Mostly it’s just an annoyance, but some non-techie users can be confused by the message as well.

Want to close your own darn window without prompting the user? Use this cheater script. I’ve only tested it on Trusted sites, so no promises about Internet Zone sites, mmkay?


<script type="text/javascript">
function closeMe()
{
window.open('','_self','');
setTimeout("self.close();",1000); // 1 second
}
</script>

Javascript snippet: URL ($_GET) parameters

Monday, October 22nd, 2007

Quick and dirty sample of parsing URL parameters ($_GET) from the URL and setting a form value equal to one of them. Useful for pre-setting form values, but do beware of possible form hacks. You should always strip html (or at least script tags and variations) from your form submissions on the server-side.


<!doctype html public “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html>
<head>
<title> New Document </title>
<script type=”text/javascript”>
var querystring = new Array(); // if no querystring, will be an empty array, preventing any null reference issues
if (window.location.search)
{
var pairs = window.location.search.substring(1).split(”&”);
for (var i=0; i<pairs.length; i++)
{
querystring[unescape(pairs[i].split(”=”)[0])] = unescape(pairs[i].split(”=”)[1]); // unescape encoded values like %20 for spaces
}
}
function fillForm()
{
document.getElementById(”txtName”).value = typeof(querystring["txtName"])==”undefined”?”":querystring["txtName"];
}
</script>
</head>
<body onload=”fillForm()”>
<form id=”f1″>
<input type=”text” name=”txtName” id=”txtName” />
</form>
</body>
</html>

Happy coding!

5 quick javascript tips

Tuesday, September 18th, 2007

The UseableType Weblog has a post showing us what the author considers 5 useful Javascript tips.

While I can’t say I agree with him on what he considers more readable (anonymous functions and joining arrays are more readable?? For WHOM?), they are definitely worth a read for anyone who codes Javascript. The loop optimizer was especially interesting.

Personally, I’ll take more readable and maintainable over minor performance benefits. Major benefits, though, are always worth checking out. The point on using the submit event of a form, not the onclick of the button, I agree with 110%.

Go check them out and see what you think.

Nine Javascript Gotchas

Saturday, September 1st, 2007

Fitzblog has posted a great article, Nine Javascript Gotchas. If you’ve done much development for the web that uses javascript, you’ve likely encountered at least one of these and spent hours giving yourself a new hairstyle. Perhaps some bumps on your forehead from hitting the desk, too.

My favorites in the article:
2) The adulterous “this” can change what it refers to.
and
5) MouseOut sometimes means MouseIn

The 6) ParseInt scoffs at your base ten numbering system is a great reason to just always use the base 10 in your call to parseInt.

Check out the other 6 points at Nine Javascript Gotchas

javascript: setInterval

Saturday, June 30th, 2007

If you’ve been using setTimeout, check out setInterval, too.

Most online JS examples use setTimeout, but if you find yourself looping constantly to have something happen every XX seconds or minutes, what you really wanted was setInterval.

MSIE
Gecko/Firefox

Cool, eh?