3

I am experiencing a strange issue with javascript and firefox 10.0.11. I have tested with IE and am unable to replicate the issue.

I have a link which goes to a page that shows some information about an object. I have added additional features to this page using javascript. If javascript is enabled, the anchor link will be redirected to a javascript function instead.

<a href="/Comment?id=1186281" onclick="return CommentSubmit(1186281)">Comment</a>
function CommentSubmit(id) {
    $("#DynForm").append("<input type='hidden' name='id' value='" + id + "' />");
    $("#DynForm").attr("action", "/Comment/Index");
    $("#DynForm").submit();
    return false;
};

As you can see, the javascript inserts a hidden input tag with the key value. The javascript-free version works fine, but i experience some issues with the javascript. When clicking the Comment button for any item the first time, it works fines. If I hit back then click any Comment link, the page shows up as if I clicked the first link again. Upon inspection, I noticed that the url parameters get 'stacked' for each subsequent 'Back' and 'Click'

/Comment?id=1
/Comment?id=1&id=2
/Comment?id=1&id=2&id=3

If I navigate to the page again (without going back), the first link will work again then start this strange behavior. Looking at the page source after clicking a few links, I see no new hidden fields that would add these additional parameters. Is this a known issue? How can I further debug this and fix it?

1 Answer 1

6

This isn't a bug, it's a feature :)

Your browser is caching the page, so when you go back you're not refreshing it, you're returning to it as it was, i.e., with the hidden input field.

All you need to do is remove that field before you append it again.

function CommentSubmit(id) {
    $("input[name=id]").remove();
    $("#DynForm").append("<input type='hidden' name='id' value='" + id + "' />");
    $("#DynForm").attr("action", "/Comment/Index");
    $("#DynForm").submit();
    return false;
};
Sign up to request clarification or add additional context in comments.

2 Comments

+1. This is indeed correct that it is a feature (no sarcasm). If you have multiple fields with the same name your are implying a collection and therefore you get the resulting url. Working as intended.
@FrançoisWahl Very interesting. I had suspected browser caching, but I ruled that out since the actual URL is changing for each request. I had not thought about the browser caching the search page. Since the hidden id field is added on click, does this imply page caching happens when leaving the page? Why is it the case that when I hit 'Back' I am unable to find the appended hidden fields?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.