views:

35

answers:

2

Hi,

Can you check if this is a browser cache problem?

I have this scenario, I have a table that displays data like this and below that is a link that sends you to another url. Only one row can be click at a time.

<table>
    <thead>
        .
        .
    </thead>
    <tbody>
        <tr>
            <td><input type=""checkbox" value="1" name="_chk"/></td>
            <td>Field1</td>
            <td>Field2</td>
        </tr>
    </tbody>
</table>
<a href="/myApp/url.htm" id="myLink">Change</a>

On click of the link, I am adding the current click id to the href attribute using jquery

$(document).ready(function(){
    $("#myLink").click(function(){
        var transID = $("input[name='_chk']:checked").val();
        var currHref = $(this).attr("href");

        $(this).attr("href", currHref + "?transID=" +transID);
    });
});

On first click, its ok and transaction id gets appended as parameter to the new url string like this /myApp/url.htm?transID=1?

Problem is this, when I click the back button and then click another row, the url parameter string gets doubled like this /myApp/url.htm?transID=1?transID=2

Is this a browser cache problem and is there any workaround for this?

+2  A: 

Yes, this is a javascript/html state cached by browsers. If you are passing values from what should have been form controls between pages, I suggest you use the default way which is to use form and a submit button.

If you insist in using Javascript, then don't get the attribute "href", hard code the url (don't get it from href attribute) and append the parameters and redirect to that page using Javascript.

Try this if it works (but this is just a quick fix to your code):

$(document).ready(function(){
    $("#myLink").click(function(){
        var transID = $("input[name='_chk']:checked").val();
        var currHref = "/myApp/url.htm";

        $(this).attr("href", currHref + "?transID=" +transID);
    });
});
Manny
+1  A: 

Try this...

$(document).ready(function() {
    var baseUrl = "/myApp/url.htm";

    $("#myLink").click(function(){
        var transID = $("input[name='_chk']:checked").val();
        $(this).attr("href", baseUrl + "?transID=" +transID);
    });
});
Drew Wills
@Drew. Just one question. I added below lines in all my HTML Head Tag <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Cache-Control" content="no-cache"> <meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">. Will this be not enough to prevent browser cache?
Mark Estrada
@Mark Estrada: I don't think your issue is with cache headers or meta tags. When you use the back button, I *suspect* the browser will give you the previous version of the page no matter what (except perhaps on a POST). This behavior might be different between browsers and it might just be more nuanced than I understand. But my solution and Manny's solution will fix it anyway.
Drew Wills