* updated 27th Feb 2014 based on feedback, see below *
Quotes are fun, random quotes more so.
- Inserts a random quote into a quote block page, when a quote block is configured for random insert.
- Other quote blocks not configured for randomness will be untouched.
demo: (refresh page to see a different quote)
- add a quote block on any page using quote text "Etaoin" with attribution "Shrdlu"*
- get list of quotes together and replace the list at the top of the footer code (below)
add the footer code to the site wide footer
the key phrases can be changed to anything you choose such as "eat me" and "drink me" for instance, just remember if the phrase appears in the page outside a quote block it will still get replaced.
How This Works
- the page loads displaying the quote block with quote text "Etaoin" with attribution "Shrdlu"
- the footer loads and executes the code
- a random quote is chosen from an embedded list
- the page content is grabbed into a variable
- a simple search and replace is performed for the two keywords, placing them with the quote and attribution text
- the updated page content is inserted, replacing the original
- the random quote is now visible
- Sometime if the page loads slowly due to poor connection or any number of issues the reader may briefly see quote text "Etaoin Shrdlu" before it's replaced. Perhaps you could look at this as an opportunity to add some mystery to your pages...
- If your keywords are added to other content they will get replaced as well, make sure you chose something unusual and be mindful when composing content.
- If you have more than one block quote configured on a page, they will all be replaced with the same randomly chosen quote. However, why would you have more than 1 random quote block per page?
- The design assumes certain page structures are in place and leverages those to make it work. If they're not present because the template is something new or it's your own custom design, it may not work.
- You could load the quotes from an external file, hosted say in dropbox or any CDN based storage, however a secondary call, parsing then processing will result in a delay in replacing the quote.
- Instead of doing 2 search and replaces for the quote and attribution on all of the content, we could grab all elements that are quote blocks and cycle through them to avoid potential clashes with other content, however well chosen keywords will obviate this issue if you are mindful of them.
This code will be maintained on github.
<script> var myQs = new Array(); myQs = "quote 1~unknown"; myQs = "quote 2~unknown"; myQs = "quote 3~unknown"; myQs = "quote 4~unknown"; myQs = "quote 5~unknown"; myQs = "quote 6~unknown"; myQs = "quote 7~unknown"; myQs = "quote 8~unknown"; myQs = "quote 9~unknown"; myQs = "quote 10~unknown"; myQs = "quote 11~unknown"; var range = myQs.length; var index = Math.floor(Math.random()*range); var quote = myQs[index]; var res = quote.split("~"); quote = res; var attribution = res; var bodyGrab = document.getElementById("page").innerHTML; bodyGrab = bodyGrab.replace("Etaoin", quote); bodyGrab = bodyGrab.replace("Shrdlu", attribution); document.getElementById("page").innerHTML = bodyGrab; </script>
- Added 4th known issue regarding template/page structures
- Changed Footer code from using content block to page block so now this works with more squarespace templates. If you know what this means and you stilll have an issue you can fix this issue yourself if you so desire, or get in touch