views:

70

answers:

3

Hi,

I have the following script to post some stuff to the server and write the json result back to the users browser. It doesn't work as expected :(

$(document).ready(function () {
    $('#productForm').ajaxForm({
        dataType: 'json',
        success: function (response) {
            var tmp = '<tr>';
            tmp += '<td><a href="/Invoice/DeleteProduct/' + response.Id +'>Kasta</a></td>';
            tmp += '<td>' + response.Quantity + '</td>';
            tmp += '<td>' + response.UnitPrice + '</td>';
            tmp += '<td>' + response.ProfitRate + '</td>';
            tmp += '<td>' + response.Description + '</td>';
            tmp += '<td>' + response.Total + '</td>';
            tmp += '</tr>';
            $('#productsBody').append(tmp);
            alternateRows();
        },
        clearForm: true,
        resetForm: true,
        timeout: 3000
    });
}); 

In firefox all is well, in chrome the new row doesn't show at all but I some tiny area changes color.

In internet explorer 8 I don't get the link, it treats the link as regular text. I'll go scream some more and hope someone can tell me why it doesn't work when I come back from screaming.

+8  A: 

Try changing

tmp += '<td><a href="/Invoice/DeleteProduct/' + response.Id +'>Kasta</a></td>';

to...

tmp += '<td><a href="/Invoice/DeleteProduct/' + response.Id +'">Kasta</a></td>';
Drew Wills
ouch, those are the worst sometimes :)
BioBuckyBall
Well spotted, this is probably it!
Pekka
I hate it when that happens!
Buggabill
Make sure you use the browsers' DOM inspectors. They help with finding out stuff like this.
Pekka
Bacon saved! I was about to jump and since I live on the second floor I would have just hurt myself. @Pekka, thanks for the suggestion about DOM inspectors. I'll make good use of that in the future! :)
mhenrixon
+2  A: 

This could be a <tbody> issue: Browsers append a tbody to table constructs automatically that you are breaking by what you are doing. Not sure, but you could try wrapping a tbody around the tr and see whether it behaves any better. Maybe somebody else has a different idea.

Other than that, Chrome's "Inspect Element" is your friend: It should be able to tell you what part of the markup gets garbled.

Pekka
I'll use the inspect element like there was no tomorrow! :)
mhenrixon
A: 

It can't be done. Even if you get it working with all browsers today, it can easily break in tomorrow's browser with the next releases. (Assuming there's a new version of jQuery ready which fixes any possible issues, if not I guess you'd have to wait and just tolerate a broken website.)

The only 100% certain answer is: Don't use JavaScript for serious development if you need a consistent platform, use a VM solution like Silverlight, Flex, or JavaFX. JavaScript/AJAX/DHTML should only be used on non mission-critical stuff which you can afford to have broken for a few days.

Crusader