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

confirm then redirect

Status
Not open for further replies.

kristinac13

Programmer
Dec 22, 2005
27
US
I have links that are dynamically placed on my page. At the moment, when the links are clicked the page goes to that link. I have changed the code so a confirm dialog comes up first saying "Are you sure you want to leave this page?" to give me a chance to reset some session variables. The problem is, once the user says "OK", I cannot figure out how to tell the page to go on to the link that was selected. I can't hard code the link as there are 7 dynamically created links that all require the confirm box. Is there a way to say "if (confirm) then go ahead and go where you were going?" lol. Any suggestions would be appreciated thanks....
 
Yes I used an attach function that should make the onclick events of my links use my confirm function, however it doesn't work. The function runs but when I click the links I do not get my confirm dialog it just goes straight to the link...
 
well then we can't be of much help. if your function "doesn't work", paste your function and your calling methods and we'll help you debug it...



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
No I'm sorry what I mean is adding something to the onclick event of my links seems to have no effect. That's what doesn't work. What I have is

<code>
function doCoolAttachThing() {
var a = document.getElementsByTagName("a");
for ( var i = 0; i < a.length; i++ )
a.onclick += "captureMousedown()";
alert("there are " + i + " links");
}

function captureMousedown(evt){
if (evt) {
mouseClick = evt.which
}
else {
mouseClick = window.event.button
}
if (mouseClick == 1){
if (confirm("Are you sure you want to leave this page?")){
document.myform.status.value = '4';
document.location = url;}
}
}
</code>

I run the doCoolAttachThing() at the onload event of my body. I can't put the onclick event directly into my links as they are brought in dynamically from a <%HeaderString%> arrangement. Someone suggested the doCoolAttachThing which seems to run just fine - I get my little link count message - but the OnClick of my links actually does nothing. Oh and I have tried other events besides the captureMousedown event...it doesn't seem to matter what I use the onClick event doesn't appear to fire. If that makes any sense....
 
that someone was me, but the code seems to have changed. my suggestion was to use this line:

Code:
a[i].onclick = captureMousedown;



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Thanks very much good I'm glad it was you...Wanted to thank you for the suggestion. I will try it this way but I think I did already. After awhile you forget what you've tested and what you haven't, but I thought that was my first attempt. I'll try again. Oh and obviously I didn't use the right tags to enter code in these windows sorry...
 
oh, you might want to try something like this (now that i think more about it):

Code:
a[i].onclick = function() { return confirm('are you sure?'); };



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
That helps!! Thank you so much. Now my only problem is that apparently the status is not what I need to change to clear my session variable. See if you leave this page via these links and then come back to this page, it reloads and reloads over and over UNLESS you have left the page by using the CLOSE button. The user has to be able to leave the page using either the links OR the close button. Judging by the person's code I was under the impression that if the status was set to 4, the user could come back without any leftover variables from the first visit. I was wrong as your code does exactly what I wanted but it doesn't solve my reloading problem. Thank you so much tho I am so much closer...
 
Hey I just wanted to thank you again for helping me. It turns out your suggestion with function() works great and I figured out what to put in that line to reset my page properly. It's working finally thank you!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top