Problem with onclick statement within asp/vb code

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


<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

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:


<form id="frmSort">

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


<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.


<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.


<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.

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.

Look at what is happening in your original. I’ll break it down line by line.


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:


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:


...
    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.

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.

Greetings again Paul,

Your first solution worked like a charm.


<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:


<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?


<%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

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