SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with onclick statement within asp/vb code

    I am having the following problem with the " Next " link not working in Firefox, Chrome, Opera.

    Code:
    <td>
    <%If Cint(iMore) > 0 then
            if iMore > iRecsPerPage then iMore = iRecsPerPage
            Response.Write("<a href=""#"" onclick=""javascript:document.frmSort('page').value=" & iPage+1 & ";document.frmSort.submit();"">")
            Response.Write("Next</a> (" & iMore & ") >>")
      Else
            Response.Write("&nbsp;")
      End If%>
    </td>
    The funniest thing is that the " Next " link works in Internet Explorer only.

    Any help would be greatly apperciated.

    Thanks,
    David

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    document.frmSort is the problem. That is an old way for IE to access HTML elements via their name attribute.
    I presume that you have a name attribute on your form element? That's a bad bad way of doing things.

    What works is to use a unique identifier on your form instead:

    Code html4strict:
    <form id="frmSort">

    Now you can use document.getElementById('frmSort') to access that element via its unique identifier.

    Code asp:
    <td>
    <%If Cint(iMore) > 0 then
            if iMore > iRecsPerPage then iMore = iRecsPerPage
            Response.Write("<a href=""#"" onclick=""javascript:var form = document.getElementById('frmSort'); form.elements.page.value = " & iPage+1 & "; form.submit();"">")
            Response.Write("Next</a> (" & iMore & ") >>")
      Else
            Response.Write("&nbsp;")
      End If%>
    </td>

    Let's try and tidy that up a bit though.

    The javascript: part does not do what you think it does. Everything in the onclick attribute is already considered to be a script. Here's the spec for it.
    So, what your javascript: is actually saying is: "I am a scripting label, one that can be used for goto commands such as break or continue. You don't want to do that.

    It's also cleaner to put the scripted contents in a function.


    Code asp:
    <td>
    <%If Cint(iMore) > 0 then
            if iMore > iRecsPerPage then iMore = iRecsPerPage
            Response.Write("<a href=""#"" onclick=""nextPage(" & iPage+1 & ");"">")
            Response.Write("Next</a> (" & iMore & ") >>")
      Else
            Response.Write("&nbsp;")
      End If%>
    </td>
    <script type="text/javascript">
    function nextPage(page) {
        var form = document.getElementById('frmSort');
        form.elements.page.value = page;
        form.submit();
    }
    </script>

    And if you want to really clean up your HTML code, you can put a unique identifier on the next page link, and use scripting to attach the onclick event on to that next page link.

    Code asp:
    <td>
    <%If Cint(iMore) > 0 then
            if iMore > iRecsPerPage then iMore = iRecsPerPage
            Response.Write("<a id=""nextpage"" href=""#"">")
            Response.Write("Next</a> (" & iMore & ") >>")
      Else
            Response.Write("&nbsp;")
      End If%>
    </td>
    <script type="text/javascript">
    function nextPage(page) {
        var form = document.getElementById('frmSort');
        form.elements.page.value = page;
        form.submit();
    }
     
    var nextpage = document.getElementById('nextpage');
    if (nextpage) {
        nextpage.onclick = function () {
            nextPage(<%iPage+1%>);
        };
    }

    Take things as far as you want to go.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Paul for your reply,

    You'll have to excuse that I am new to javascript, asp and visual basic. But will your re-arrangement of the javascript still let it work against a database query? As this is what that line of code actual is doing. The " next page " isn't a physical page its a database query dynamic page.

    I will try your code and see if this fixes the problem.

    Thank you again Paul for your help and suggestions.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Look at what is happening in your original. I'll break it down line by line.

    Code:
    var form = document.getElementById('frmSort');
    form.elements.page.value = " & iPage+1 & ";
    form.submit();
    Then look at the nextPage function that is used in the updated code:

    Code javascript:
    function nextPage(page) {
        var form = document.getElementById('frmSort');
        form.elements.page.value = page;
        form.submit();
    }

    It will work the same as long as the page variable is <%iPage+1%>
    Which is done here:

    Code javascript:
    ...
        nextPage(<%iPage+1%>);
    ...

    So, even though I do not have your development environment to test things with, the theory is solid as a rock and dependable as a brick.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Greetings Paul,

    Thank you again and for breaking down per line as to what it does. I will test it out and will let you know how it goes.

  6. #6
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Greetings again Paul,

    Your first solution worked like a charm.

    Code:
    <form id="frmSort">
    
    <td>
    <%If Cint(iMore) > 0 then
            if iMore > iRecsPerPage then iMore = iRecsPerPage
            Response.Write("<a href=""#"" onclick=""javascript:var form = document.getElementById('frmSort'); form.elements.page.value = " & iPage+1 & "; form.submit();"">")
            Response.Write("Next</a> (" & iMore & ") >>")
      Else
            Response.Write("&nbsp;")
      End If%>
    </td>
    I appreciate your excellent help with this. I do have another question though,
    once the user clicks on the next button, to get back to the previous page, I am assuming it would be the same code but with different variables correct?

    For example the original code for the " Previous Page " link:
    Code:
    <form id="frmSort">
    
    <%If iPage > 1 then
           Response.Write("<< <a href=""#"" onclick=""javascript:document.frmSort('page').value=" & iPage-1 & ";document.frmSort.submit();"">")
           Response.Write("Previous</a> (" & iRecsPerPage & ")")
    Else
           Response.Write("&nbsp;")
    End If%>
    And I would replace it with the following correct?
    Code:
    <%If iPage > 1 then 
           Response.Write("<< <a href=""#"" onclick=""javascript:var form = document.getElementById('frmSort'); form.elements.page.value = " & iPage-1 & "; form.submit();"">")
           Response.Write("Previous</a> (" & iRecsPerPage & ")")
    Else
           Response.Write("&nbsp;")
    End If%>
    The reason I ask this about the " Previous Page " is when I replaced the code with the new when it gets back to the starting search page, there is still a " Previous " link there when there shouldn't be and it attempts to go back another 15 records when its at the first 15 starting records.

    Any suggestions?

    Thank you,
    David

  7. #7
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Paul,

    I have it figured out, it was a syntax error on my part with identifying the iPage.

    Again thank you for your quick response and your help. Hopefully one day I'll learn what all the javascript, asp and visual basic stuff means and does.

    Have a blessed day

    Thank you,
    David


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •