Keeping track of the page that the user entered through
I'm trying to figure out how keep track of the incoming page a visitor initially entered through and show that as a link throughout the site so that they can return to that initial page if they are navigating around the si开发者_运维问答te. Essentially the client's model is that they they're going to email links to a potential customer to a specific product page. They didn't build a navigation on the site with all their products (bad idea I know). But should the user leave that initial product page after entering through the email link and navigate to the contact page, homepage or wherever else, there's really no way unless they continually click back to find that initial product page they entered through.
So I'm using a PHP CMS (MODx specifically) and using Foxy Cart ecommerce. As such, I've got jquery and JSON at my disposal to work with. Unfortunately I have no idea where to start.
Here would be what I assume I need to do:
- Have an empty div that's waiting in a sense to receive the incoming link.
- Upon entering the product page, the jquery would "grab" the value in the h1 tag and grab the URL as well, store that info the empty div mentioned above.
Here's what I would envision it to be (please feel free to correct me if I'm wrong or if you have a better idea):
<h1>Product Name Here</h1>
<div class="records_first_page_entry_name"><a href="[+first_page_URL+]">[+product_name_here+]</a></div>
I would assume that these placeholders (namely [+first_page_URL+] and [+product_name_here+]) would be replaced by their respective values. Then I would need this div's values to be carried throughout all the pages of my site.
I hope I'm explaining myself correctly. Thanks!
If you can only use javascript then use cookies
example: http://jsfiddle.net/kEpnW/ (first time it saves the data, second time it shows them so reload the page once)
Part to check if cookie exists (show info) or not (create cookie on first visit)
$(function(){
var first_page_title = Cookies['first_page_title'];
var first_page_url = Cookies['first_page_url'];
if (first_page)
{
var ahref = $('<a>',{text: first_page_title, href: first_page_url});
$('.records_first_page_entry_name').html( ahref );
}
else
{
Cookies.create('first_page_title', $('h1').text(), 0);
Cookies.create('first_page_url', window.location.href, 0);
}
});
Cookie handling code
var Cookies = {
init: function () {
var allCookies = document.cookie.split('; ');
for (var i=0;i<allCookies.length;i++) {
var cookiePair = allCookies[i].split('=');
this[cookiePair[0]] = cookiePair[1];
}
},
create: function (name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
this[name] = value;
},
erase: function (name) {
this.create(name,'',-1);
this[name] = undefined;
}
};
Cookies.init();
You need to include these on all pages..
Alternatively, you can use PHP session variables to store the same info and display it in your pages.
Do you really need to get the specified url? You can navigate back with window.history.back()
. An other option is to retrieve the site server side but I don't know if it is possible with jQuery.
精彩评论