Forum Moderators: open

Message Too Old, No Replies

Pagination script quickie

         

rideforever

8:12 am on Dec 20, 2007 (gmt 0)

10+ Year Member



Hi there,

I've hunted the web for a good pagination script and come across what [i]may[i] be a cracker but I'm having some problems in implementing it. Take the code below:

var pageCurrent = 1;
var objPage = null;
function writePageControls() {
document.writeln (’<div class="toolBar">’);
document.writeln (’<div class="pagination">’);

Problem ONE: Firefox states that line 4 contains an "illegal character." Really? Where? After comparing several examples online I can't find out where.

Problem TWO is with the following code below:

<script type="text/javascript" language="javascript">
writePageControls();
</script>

Firefox claims this function is "undefined". Could somebody please point out what that means.

I'm admittedly new to Javascript and I'm working hard to try and implement a pagination script for a very long article. This script was originally taken from:

[peachpit.com...]

The article is great but contains a few typos and the above issues that I'm working through. Any help really appreciated.

Thanks alot
Stephen

penders

1:31 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



document.writeln (’<div class="toolBar">’); 
document.writeln (’<div class="pagination">’);

Should read:

document.writeln ('<div class="toolBar">'); 
document.writeln ('<div class="pagination">');

The article you've copied the code from has used the wrong character (may be for visual effect; I don't know?!) - they've used the closing single quote, whereas it should be the apostrophe. Unfortunately, if you simply copy & paste their code, it won't work!

Firefox claims this function is "undefined".

It simply can't find the function. This may be due to the above syntax errors - as the JavaScript could not be parsed correctly. Usually though this would be because the function is not defined in the current page (head section), or included (if in an external file).

Fotiman

5:39 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Also, I question the quality of any script that uses document.writeln or document.write. These methods should generally be avoided in favor of using DOM methods for creating new HTML elements.

rideforever

8:25 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



Thanks Penders, that worked a charm. It was just a case of swapping over the Single Quote for the apostrophe, and the script works out fine. Must have been a formatting error in the article that caused this.

rideforever

8:32 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



Sorry Fotiman, just seen your post. How would you alter the script then exactly? Just for your info, I've pasted the main body of the script into the code-bin below:

[code-bin.homedns.org...]

Javascript is very new to me (though seems straightforward), but I'd be interested in your opinion to improve the script.

The original article is linked above

Fotiman

6:27 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How would you alter the script then exactly?

First, paging is really something that is best done server-side in my opinion.

As for this example, though, I would remove any script that was embedded inside the HTML content. For example:

<script type="text/javascript" language="javascript">
writePageControls();
</script>

Instead, do something like this in your <head> or right before the closing </body> tag:


<script type="text/javascript">
window.onload = function() {
writePageControls();
};
</script>

Note, I've removed the "language" attribute as it's not valid and does nothing.

Your writePageControls method should then generate a bunch of elements and insert them into the DOM using methods like document.createElement, document.createTextNode, and appendChild. See the DOM Core spec:
[w3.org...]
or the HTML DOM tutorial at w3schools.com:
[w3schools.com...]

By moving your javascript out of your HTML content, it will be easier to maintain. Keep your content and behavior separated.