开发者

Creating Javascript History like Object in Coldfusion

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 ca开发者_如何学运维use Leaving my website.

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


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜