Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need redirect script to bypass page after it's been cached?? 1

Status
Not open for further replies.

jamsa

Programmer
Oct 5, 2001
18
US
I have a link to a portfolio page that first takes a user to a copyright "agreement page" which then requires a click-through to get to the actual portfolio page. However, once they have done this, I don't want them to have to go there again later.

I am interested in putting a javascript command that will redirect, or essentially bypass, the copyright page once a user has already been there (hence, the copyright page has been cached.) This is so once they have "agreed", if they then reaccess the portfolio page from another part of the site, the script will bypass the copyright page and go directly to the portfolio page.

I assume this script would go in the source code on the copyright page.

Or, is there another way?

Please help me with this one!

jamsa
 
So what you're saying is that you're comfortable with a computer accepting the agreement page and not the person ?? If this is the case, I would suggest that you have JavaScript write a cookie to that computer when the agreement page is accepted. You can set the expiration date for that cookie as needed. Then, when ANY vistor on that computer wants to go back to that page, have JavaScript read the cookie file to determine if the agreement has already been accepted, then redirect as necessary.

Hope this helps.

TW
 
Thanks Todd-

No, what I'm looking for is a way to redirect a user (computer) during a current session, or as long as the site is cached on the user machine.

Any ideas?
 
Well, unless you're using a server side scripting solution to maintain session state, you're either going to have to format headers to expire the document from cache (which has it's loopholes) or re-look at the solution that I provided above. You can write a cookie to the computer and tell that cookie to expire, say in 1 hour. That way, during that hour, you can have JavaScript examine that cookie, and if present, redirect the visitor as needed. One hour later, the cookie would be expired and you could present the agreement page again. Now I don't use any of these solutions, so I don't have experience with a working solution like this. But I also know that you can set a cookie to expire when the user closes the browser software. That may be a better solution for you. The idea being, you write a key/value pair to the cookie file when the agreement page is loaded. Then, as long as the web browser is open, the cookie file will be there for you to examine. Once the browser software is closed, another examination of the cookie file will tell you that it is a new session (so to speak).

Are you using a server side technology like ASP ?? If you are, you could use session variables to prevent a user from viewing an already viewed page during a current session very easily. That would be the easiest way. Otherwise, I think cookies would be the best solution over attempting to manipulate the cache expiration of a document.

Sorry, I feel like I'm slipping away from my expertise here.

TW
 
Oh, by the way. Even if you could successfully expire a document from cache when you wanted, browser security prevents you from reading history pages in cache, so there would be no way to determine if the file is cached in the visitors computer (that I know of)..

Cookies or server side session management is going to be your best bet.

TW
 
OK, I see.

Thanks, Todd, this sounds like it will cover my needs. My next challenge is to come up with the code for such a purpose. I could use any "guidance" available on that process!

So, thanks for your time and interest.

james
 
I don't write cookies on my application, so I don't know the code off the top of my head. I do know that there is a cookie object in JavaScript. Try re-posting a new question and asking for some tips on adding, updating, expiring and removing cookies on a vistors computer through JavaScript.

I think it's pretty simple.

TW
 
This is the code I use for cookies. If you find any bugs please feel free to report them to me.

Code:
<script language=&quot;javascript&quot;><!--

// use to set a cookie
// name and value are required parameters
// expir, path, and domain are optional
// expir is number of days until it should expire, defaults to end of session (closing of browser)
function setCookie(name,value,expir,path,domain)
	{
	var name_value = name + &quot;=&quot; + escape(value);

	if (!expir) expir = &quot;&quot;;
	else
		{
		var D = new Date();
		var mill = D.getTime();
		D.setTime(mill + Math.round(expir * 86400000));
		expir = &quot;; expires=&quot; + D.toGMTString();
		}

	if (!path) path = &quot;; path=/&quot;;
	else path = &quot;; path=&quot; + path;

	if (!domain) domain = &quot;&quot;;
	else domain = &quot;; domain=&quot; + domain;

	var full = name_value + path + domain + expir;
	document.cookie = full;
	}

// erases the cookie
function killCookie(name)
   {
   var D = new Date();
   D.setTime(10);
   var full = name + &quot;=; expires=&quot; + D.toGMTString() + &quot;; path=/&quot;;
   document.cookie = full;
   }

// returns the string value of the cookie based on the name parameter passed to setCookie()
function getCookie(name)
   {
   if (document.cookie) var C = document.cookie;
   else return &quot;&quot;;
   var pair = &quot;&quot;;
   var arr = C.split(&quot;; &quot;);
   for (var i=0;i<arr.length;i++)
      {
      if (arr[i].indexOf(name + &quot;=&quot;) == 0) pair = arr[i];
      }
   if (pair == &quot;&quot;) return &quot;&quot;;
   var result = pair.substring(pair.indexOf(&quot;=&quot;) + 1, pair.length);
   return unescape(result);
   }
//--></script>

Use code such as this to set the cookie

Code:
<script language=&quot;javascript&quot;><!--

if (getCookie(&quot;visited&quot;) != &quot;yes&quot;)
setCookie(&quot;visited&quot;,&quot;yes&quot;);
else location = &quot;newpage.html&quot;;

//--></script>
 
Thanks, Petey

So, bear with me, I have a tiny bit of experience with j.s.

So do I just paste the whole script you gave me into my code? and where do I need to supply my own names etc.?

All I need is to redirect to a different page during one session, so that the first time a user accesses the portfolio section he/she sees the copyright info page, then after that when he/she cliks on the portfolio link from anywhere in the site, the redirect will bypass the copyright info page.

If you'd like to see the site I'm referring to here it is:


thanks
james
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top