The Developer Day | Staying Curious

TAG | JavaScript

Internet Explorer has a strange issue regarding http referers. If you would have a link and would follow that link $_SERVER['HTTP_REFERER'] value would get populated. But if a redirect is done using javascript like this:

top.location.href = 'page.php';

The HTTP_REFERER will not get value populated. I tried to figure out why is this happening but seems it’s just the way it is. All other browsers Firefox, Chrome, Opera will set the referer properly, but IE6, IE7 and IE8 will not.

Though it is not a good practice to rely on HTTP_REFERER and you should probably not use it if you can, but nevertheless such kind of behaviour may cause strange bugs. If you happen to know why Internet Explorer behaves like this or know workaround to still have a referer value set when the browser redirects using javascript please leave a comment.

, , , Hide



Javascript Radio Group Value Tip Using Prototype

A simple tip how to find out radio group value with prototype:

var value;
Form.getInputs(this.form, 'radio').each( function(input) { if (input.checked) { value = input.value }; });

, , , Hide



Javascript forms innerHTML issue

If you would do something like this while validating a form:

document.getElementById('your_form_id').innerHTML += 'any text';

Your form will get reseted and there’s nothing more to validate.

, , Hide



JavaScript Associative Arrays & Iterations

Today I was working with a little piece of JavaScript code and writing something like this:

var tmp = new Array();
tmp['id']= '123';
tmp['name'] = 'bla bla';

And then i tried to iterate over the array like this:

for (var i in tmp) { alert(i); };

If you haven’t done a lot of JavaScript you would probably say that the above iteration should output ‘id’, ‘name’. Surprisingly you may be wrong. If you are using any code that extends the Array prototype with attributes or functions you will see them in the output. This may happen if you are using the prototype framework or The open source code of a JSON parser and JSON stringifier. All you’re doing is setting properties on an object. After reading a few blog posts and comments on this I found yet another issue that ideally I shouldn’t be using new Array() and using new Object(). That sounds kind of true because JavaScript does not support associative arrays and you could do the same on any object. Date, RegExp, Boolean or any other. Actually using Object solved the prototype framework problem, because prototype framework does not extend the Object prototype since 1.4 but the JSON stringifier does.

After googling a while I found out a little tricksy that may mostly work with this problem. You could do something like this:

for (var i in obj) {
if (!obj.propertyIsEnumerable(i)) continue;

This only works if properties are enumerable. That may not always save you especially if you are using any 3rd party software that breaks this, but it certainly helps you while solving the prototype framework or JSON stringifier puzzle.

, , , , Hide

Find it!

Theme Design by