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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

keeping script from running on refresh

Status
Not open for further replies.

wrbodine

Programmer
Aug 24, 2000
302
US
Is there any way I can keep script from running when a page is refreshed? I have some code that enters a row into a database, and don't want it to be called again if the user refreshes the page.

Maybe if there's a way to identify if the page was just refreshed? I haven't seen any helpful threads on how to disable refresh, so that's probably out....

TIA,
Ray
 
Ray,

I've got this problem with some scripts I've run...which are DB-driven, so if the page is refreshed, they duplicate adding a DB record. I just either use:

1) Response.Redirect("pagename.asp")
2) a JavaScript link IN BIG-LETTERS directing users them elsewhere with a warning about refreshing the page

#1 is probably safer...you can just execute your script on PAGE1.ASP, and Response.Redirect to PAGE2.ASP right away, using this latter page to display a confirmation message thereby avoiding the chance of error.

HTH,
Jason
 
You could store the scriptname in a hidden area on your page, such as a hidden form element.

When the page loads, it could do a
MyVariable = REQUEST.FORM("myFieldName")

If MyVariable = "" Then

' Execute some stuff
Else
REsponse.Write "You just refreshed"
End if


I think that would work.

Anyone, please correct me if I'm wrong.

shorty
 
Using the Response.Redirect method is the one I have used as well.

shorty
 
You could make a session variable and before you run the script to load the DB you could check to see what is in that variable. If it's empty run the script and place a marker in the session variable, if the variable is not empty then don't run the script.

Mike
 
These are some ideas:

1. Response.Redirect method
2. Pass it in a hidden field - Request.Form
3. Pass it in QueryString
4. Use a cookie in place of the session variable
 
What about checking REFERER? This will work if it is not a frameset.

[bb]
 
I tested using document.referrer and I got back an empty string when trying to put it in an alert. Maybe my syntax was wrong somewhere. I'll test it again.
 
I don't think document.referrer will work. Tested it again and all I get back is an empty string.
 
I mean ASP before you embark on any db work check that the referer was no the same page.

It doesn't work anyway - the page keeps the same referer.
 
Thanks for all of the input everyone!

I might look into using a cookie. Response.redirect won't work, since I need to display data on the page that I'm getting from variables with Request.Form that gets them from another page. (I can't pass the variables in the query string because some of the data is very lengthy.) I've tried some variations on the code shorty wrote above but for MyVariable in his code its always giving me the same value on a refresh; its blank if it was blank the first time (even if I change the value as he did and make it a hidden variable, because the page is being populated from another page, where the action is set to the page I'm having problems with). So far with what I've tried the variable keeps the same value on refresh even when I change it later (it just changes it later after the refresh).
 
You could make a session variable and before you run the script to load the DB you could check to see what is in that variable. If it's empty run the script and place a marker in the session variable, if the variable is not empty then don't run the script.

This is the best way to do it, in my opinion:

<%
if session(&quot;loaded&quot;) = &quot;&quot; then
session(&quot;loaded&quot;) = true
'do your code here, and forget about refreshes
end if
%>

Once the session variable has been set inside that if block, the code won't be run again on the same session, and everything is good.

:)
paul
penny.gif
penny.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top