views:

198

answers:

3

I want to create a History Object for my Website, It should be Similar to Javascript history object.

I want to use this Object to Creating Previous/Next nevigational Links for my Website. The Problem with Javascript History Object is that it is based on the Window object, not a website specific. And Using this Object clicking on "Prvious" link can cause Leaving my website.

What should be the best possible way to Create a History Object in Coldfusion?

A: 

Preventing people from leaving your site is a lame and annoying thing to do. Do you have a good reason for doing it?

Justin Johnson
Because this Previous link is within my site. And user wants normally wants to navigates within the site with next and Previous links.
+2  A: 

Looking at global question that sounds like "How can I make the user surfing my site better?" I can three important answers:

  1. Obvious navigation aka main menu. Especially, when you have a lot links there. Solutions may vary: plain links, tabs, drop-down menus etc.
  2. Using breadcrumbs. People should be able to go level up (though it is not always "Back" action).
  3. History. Implementing custom history can be useful, say for e-shop -- to show previously viewed stuff in reliable and handy way.

Please note that history is the task #3, not 1 or 2. Reason of explaining all this is that your History should not serve for #1 (definitely) and #2 (can be sometimes).

Basically history can be stored in two ways: for current session only (for any user) and between sessions (typically for logged in users).

Simplest way to implement the first way is to use ColdFusion sessions. When creating session (onSessionStart() if using Application.cfc) initialize the container, I would use the array.

Consider the following samples:

<cfscript>
    session.history = [];
</cfscript>

When user opens new page (even in new tab -- which starts new browser history) -- push the page information into the container (page should contain link and kind of label at least):

<cfscript>
    page = {};
    page.link = "/index.cfm?product=100";
    page.label = "Product Foo";
    ArrayAppend(session.history, page);
</cfscript>

Finally, somewhere in page template loop over this array and display the links:

<cfloop array="#session.history#" index="page">
    <div><a href="#page.link#">#HTMLEditFormat(page.label)#</a></div>
</cfloop>

Obviously, if you want to show the Previous/Next links, you should modify the way of storing the history, maybe keep current page position (in array) too -- to pick the previous and next elements. Though as a User I would not find such feature much useful.

Finally, if you want to store the history between sessions, simply write this dataset in the database identified by user id (fk) and restore it when user logs in.

Please remember, that it is highly recommended to use locking when reading/writing.

Sergii
Using locks when reading session variables is pretty much no longer needed, and unless you're dealing with race conditions locking for writing to a session you don't need to lock then, either.
Al Everett
A: 

Are you using a link/button on your site to act as the back button?

If so, you could use javascript to hide the "Previous" link/button in your site if the history-1 doesn't contain your domain name.

edit - you can't use the history object because of security but you can use document.referrer.

<head>
<script language="javascript">
    function showBackLink(){
        var ref = document.referrer;
        var fromThisDomain = ref.indexOf("yourdomain.com");
        if(fromThisDomain > 0){ // your domain was found, show the link
            document.getElementById("backLink").style.display = "";
        }else{ // your domain was not found, hide the link
            document.getElementById("backLink").style.display = "none";
        }
    }
</script>
</head>


<body onload="showBackLink();">
<a ID = "backLink" href  = "javascript: history.go(-1);">prev</a>

</body>
Travis
Yes, I am using the Link on my site.Can you please write me an example?