JavaScript - Javascript Alert When Exiting Page
Bear with me, as I do not know where the problem lies. I know this is a lot of code. I have acquired this code and am not sure what to change to make it work how I want it to. Basically it prompts, with a Javascript alert, for the visitor to take a survey. If they hit OK it redirects them to a landing page, if they hit CANCEL it lets them go on their way.
FIRST OFF THANK YOU SOOOO MUCH FOR ANY HELP YOU CAN PROVIDE ME!!! I would like for the script to execute even when the visitor types a URL in the address bar and exits that way versus just alerting the visitor when they click an external link. That is the main goal that I need help with. I would also like the script to use cookies so that it doesn't pop up for a visitor more than once in either 7 or 14 days. This isn't as big of a deal as the problem listed above but is more of a bonus if I can get it. I have listed all pages with code that pertain to this code because, as listed above, I do not know where the problem lies. If it helps, this code is also based off of the jQuery code. Below is the page where the code executes: Code: <html> <head> <script src="SurveyRelocationManager.js"> <!-- --> </script> <script type="text/javascript" src="jquery.js"></script> <script> <!-- var srm; function init(){ srm = new SurveyRelocationManager(); srm.setSurveyURL("http://www.poddcorp.com"); //change to the correct survey URL //add the internal URLs, if internal popup does not deploy srm.addInternalURL("https://secure3.convio.net/ucs/site/Advocacy/"); srm.addInternalURL("http://ucs.convio.net/site/Ecard/"); srm.addInternalURL("https://secure3.convio.net/ucs/site/Donation/"); srm.addInternalURL("http://ucs.convio.net/site/UserLogin/"); srm.addInternalURL("http://ucs.convio.net/site/PageServer?pagename=sign_up/"); srm.addInternalURL("http://www.ucsusa.org/"); $("a").click(function () { return srm.onBeforeClick(this); }); } window.onload = init; --> </script> </head> <body> <a href="http://www.ucsusa.org/internal.html">internal</a><br /> <a href="http://www.themmrf.org/donate-now-take-action/join-an-event/powerful-athletes/mmrf-triathlon-and-cycling.html">external link</a><br /> <a href="internal.html">internal</a><br /> <a href="http://www.yahoo.com">external link</a><br /> <a href="http://www.ucsusa.org/global_warming/">another internal</a> </body> </html> Below is other code (SurveyRelocationManager.js) that helps execute the script: Code: function SurveyRelocationManager(){ //Constr. this.aInternalURLs = new Array(); } SurveyRelocationManager.prototype.init = function(){ this.setInternalURLs(); } SurveyRelocationManager.prototype.setSurveyURL = function(s){ this.sSurv = s; } SurveyRelocationManager.prototype.getSurveyURL = function(){ return this.sSurv.valueOf(); } SurveyRelocationManager.prototype.addInternalURL = function(s){ this.aInternalURLs.push(s); } SurveyRelocationManager.prototype.removeInternalURL = function(s){ for(var a = 0; a<this.aInternalURLs.length; a++){ if(this.aInternalURLs[a] == s){ this.aInternalURLs.splice(a, 1); } } } SurveyRelocationManager.prototype.getInternalURLs = function(){ return this.aInternalURLs.slice(0); } SurveyRelocationManager.prototype.getURLIsInternal = function(s){ var t; /* KLUDGE: t.indexOf returns -1 if aInternalURLs has no slash at the end. Somehow forward slashes cancels the eval. TODO: Refactor t.indexOf. */ for(var a = 0; a<this.aInternalURLs.length; a++){ t = this.aInternalURLs[a]; // if(s.indexOf(t) != -1){ return true; }else if(s.indexOf('http://') == -1) return true; } return false; } SurveyRelocationManager.prototype.onClick = function(o){ var b = this.getURLIsInternal(o.toString()); if(!b){ var c = confirm("Do you want to take the survey?"); if(c){ //TODO: Relocate here. document.location = this.getSurveyURL(); } else { return true; } } else { return true; } return false; } Note: The only other page that isn't located on this post is the jQuery code. Because of it's length it wouldn't allow me to include the code. You can visit jquery.com to view the code. Or message me and I can send it. THANKS A MILLION! Similar TutorialsI have an user table like this:- guid | username | password | firstname | lastname | location | emailad dress | userrole -----------------------------------+----------+----------------------------------+-----------+-----------+----------+-------- ------+--------------- 8024259764dc3e8ee0fb6f5.84107784 | james | 827ccb0eea8a706c4c34a16891f84e7b | james | bond | NY | ny@live .com | administrator 18689183644dc3e91571a364.71859328 | saty | 250cf8b51c773f3f8dc8b4be867a9a02 | saty | john | NY | hk@fd.c om | administrator 2644885344cecd6f2973b35.63257615 | admin | 21232f297a57a5a743894a0e4a801fc3 | System | Generated | | | administrator (3 rows) now my postgre query for delete the row .... $query = "delete from users where username!= 'admin' and guid='".$guid."'"; $result = pg_query($conn, $query); ?> <script type="text/javascript"> alert("Cannot delete this .\n It is system generated(s)."); </script> <?php (1)when I delete the user name one by one then delete occurs in my page userlist.php, I donot want to delete admin so i use username!= 'admin' in where condition as shown above. (2)now when I del any username(3 rows) from user table then alert occurs & it delete from userlist.php after that my page userlist.php is blank. Finaly when i refresh the page then my admin username seen.. when i use return true; function then only alert generate .. delete doesnot occurs ... Actauly i want:- (1)if user is not admin then it delete from userlist.php ... nd also i m continue on this page ... like when james and saty want to delte their acount ..as given in table. (2)if user is admin then alert generate nd i m continue on this page. i m tired now plz help me .... so can anyone put the best condition in my coding. The code below is simply supposed to have a menu show up whenever one of the paragraphs is clicked on. There are two options in that menu: Turn the paragraph that was clicked red or exit(hide) the menu. The problem I'm having is that I cannot find out how to get out of the two functions when exit/hide is clicked on. So if I click on Paragraph 1 and hide the menu, I want to be able to turn another paragraph red without it affecting the first paragraph. I hope this makes sense. Also, I know there are easier ways of doing this task, but my goal is not to simplify this task. I want to learn how to fix the problem as it is presented. Anyway, thanks in advance! Code: <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <script type="text/javascript"> $(".numbers").live('click', function(event){ $('.action, .menuhead').css('visibility','visible'); var num = $(this).attr('id'); $(".menuhead").click(function(){ $('.menuhead, .action').css('visibility', 'hidden'); //I need to exit both functions here }); $(".action").click(function(){ $('.menuhead, .action').css('visibility', 'hidden'); $('#'+num).css('color','red'); }); }); </script> </head> <body> <p class='numbers' id='1'>1</p> <p class='numbers' id='2'>2</p> <p class='numbers' id='3'>3</p> </div> <p class='menuhead' style='visibility: hidden'>Exit Menu</p> <p class='action' style='visibility: hidden'>Cange Color</p> </body> Code: <script> var rXL = ""; do { rXL = prompt("Enter more entries? Y/N"); // IF Y or N is entered, the loop should in theory exit } while (rXL != "Y" || rXL != "N"); </script> IF Y or N is entered, the above loop should in theory exit, however it results in a infinite loop :/. Does anyone know what I'm doing wrong? Thank you all . Hi I have create a website where many features do not support IE versions, Can someone help me on javascript code that can alert the user to use Netscape to access my website.. I mean Javscript alert code that will recommend the user to use netscape to access my website .. Any help will be much appreciated Hi Guys, seen some pages who give alerts or Warnings mentioning that i'm moving away from my webpage. Best example is hotmail when you are on the compose mail screen and when click another link it is asking for confirmation. Can you one please shed some light on how to do this. Your help is greatly appreciated. Cheers Dileep I'm still new to javascript.. My question: How and where do I add an alert that comes up to make sure the user answers all questions before the pop-up window comes up to display score and answers? Here is a link to the quiz Thanks sooo much!! Below is the code.. Code: <script language="javascript" type="text/javascript"> var done = new Array; var yourAns = new Array; var score = 0; function getRBtnName(GrpName) { var sel = document.getElementsByName(GrpName); var fnd = -1; var str = ''; for (var i=0; i<sel.length; i++) { if (sel[i].checked == true) { str = sel[i].value; fnd = i; } } // return fnd; // return option index of selection // comment out next line if option index used in line above return str; } function StatusAllCheckboxes(IDS,cnt) { var str = ''; IDS = 'q'+IDS; var tmp = ''; for (var i=0; i<cnt; i++) { tmp = document.getElementById(IDS+'_'+i); if (tmp.checked) { str += tmp.value + '|'; } } return str; } // function Engine(question, answer, opt, Qtype) { function Engine(question, answer, Qtype) { switch (Qtype) { case "RB" : yourAns[question] = answer; break; case "SB" : yourAns[question] = answer; break; default : yourAns[question] = ''; alert('Invalid question type: '+Qtype); break; } } function EngineCB(question, answer, itemcnt) { // answer is not used at this time yourAns[question] = StatusAllCheckboxes(question,itemcnt); // alert('question: '+question+' :'+yourAns[question]); } //This is the code that calculates the score. function Score(){ score = 0; var tmp = ''; var answerText = "Quiz Results<p>"; // alert('Size of QR: '+QR.length); for (var i=1; i<QR.length; i++) { answerText = answerText+"<br>Question :"+i+" Your answer: "+yourAns[i]+"<br>"; tmp = QR[i][3]; if (QR[i][0] == 'CB') { tmp = tmp+'|'; } // alert(i+' : '+tmp+' : '+yourAns[i]+'\n\n'+answerText+'\n\n'); if (tmp != yourAns[i]) { answerText = answerText+"<br>The correct answer was "+QR[i][3]+"<br>"+explainAnswer[i]+"<br>"; } else { answerText = answerText+" <br>You got this one right! <br>"; score++; } } answerText=answerText+"<br><br>Your total score is : "+score+" out of "+(QR.length-1)+"<br>"; // for varying number of questions, alter scoring var ScoreMax = QR.length-1; var ScoreInc = Math.floor(ScoreMax / 12); // Don't have fewer than 5 questions. answerText=answerText+"<br>Comment : "; if(score<=ScoreInc){ answerText=answerText+"Not quite."; } if(score>=(ScoreInc+1) && score <=(ScoreInc*2)){ answerText=answerText+"Try Again!."; } if(score>=(ScoreInc*2+1) && score <=(ScoreInc*3)){ answerText=answerText+"Rats!."; } if(score>=(ScoreInc*3+1) && score <=(ScoreInc*4)){ answerText=answerText+"Maybe better next time"; } if(score>=(ScoreInc*4+1) && score <=(ScoreInc*5)){ answerText=answerText+"Try Again!"; } if(score>=(ScoreInc*5+1) && score <=(ScoreInc*6)){ answerText=answerText+"I bet you can do better!"; } if(score>=(ScoreInc*6+1) && score <=(ScoreInc*7)){ answerText=answerText+"Hey, pretty good job!"; } if(score>=(ScoreInc*7+1) && score <=(ScoreInc*8)){ answerText=answerText+"You almost got it!"; } if(score>=(ScoreInc*8+1) && score <=(ScoreInc*9)){ answerText=answerText+"Pretty Good! "; } if(score>=(ScoreInc*9+1) && score <=(ScoreInc*10)){ answerText=answerText+"Almost! You can do it!"; } if(score>(ScoreInc*11+1)){ answerText=answerText+"You did GREAT! Congratulations."; } var w = window.open('', '', 'height=500,width=750,scrollbars'); w.document.open(); w.document.write(answerText); w.document.close(); } </script> Code: <form name="myform" class="quiz"> <ol> <script type="text/javascript"> var str = ''; var tmpr = []; var resp = ['True','False']; // allows for up to 10 responses (can have more) for (q=1; q<QR.length; q++) { str += '<li class="quiz">'+QR[q][1]+'</li><br />'; tmpr = QR[q][2].split('|'); switch (QR[q][0]) { case 'RB' : for (var r=0; r<tmpr.length; r++) { str += '<input type="radio" name="q'+q+'" value="'+resp[r]+'"'; str += ' onClick="Engine('+q+',this.value,\''+QR[q][0]+'\')">'; str += ' '+tmpr[r]+'<br />'; } break; case 'CB' : for (var r=0; r<tmpr.length; r++) { str += '<input type="checkbox" id="q'+q+'_'+r+'" name="q'+q+'" value="'+resp[r]+'"'; str += ' onClick="EngineCB('+q+',this.value,'+tmpr.length+')">'; str += resp[r]+' '+tmpr[r]+'<br />'; } break; case 'SB' : str += '<select name="q'+q+'" size="1" id="q'+q+'"'; str += ' onClick="Engine('+q+',this.value,\''+QR[q][0]+'\')">'; for (var r=0; r<tmpr.length; r++) { str += '<option value="'+resp[r]+'">'; str += tmpr[r]+'</option>'; } str += '</select>'; break; /* test code for future entries -- not implemented yet case 'CBM' : break; case 'SBM' : str += '<select name="q'+q+'" size="1" id="q'+q+'"'; str += ' onClick="Engine('+q+',this.value,\''+QR[q][0]+'\')" multiple>'; for (var r=0; r<tmpr.length; r++) { str += '<option name="q'+q+'" value="'+resp[r]+'">'; str += tmpr[r]+'</option>'; } str += '</select>'; break; */ default : str += q+': Invalid type: '+QR[q][0]; break; } str += "<p />"; } document.write(str); </script> <br /> <br /> <input type=button onClick="Score()" value="How did I do?"> </ol> </form> Hi everyone. I need a Javascript code for the following: i have a checkbox on my page. when this checkbox got checked, an alert like "Are u sure?" must ask if OK or Cancel. if i click OK the checkbox becomes checked, it runs a javascript function (chkIt() )and refreshes the page. if i click Cancel the checkbox should not became checked and my function will not run. Thanks a lot. Hi, I'm not too familiar with javascript but I am trying to find a script which I can enter a specific time and date and when that date is reached the script will alert me with a desired message. (not looking for a calendar) Help would be appreciated Harry hi i am having an issue with javascript on my web site. i am a complete noob at this. the objective is to have the page pop up an alert any time the submit button is pressed: 1st if there is no name on the form the alert says "please enter you first name" and returns a false value to the from 2nd if there is a name on the form the alert is supposed to have a thank you message my problem is that it doesn't show the alert when there is a name here is the code Code: <head> <script type="text/javascript> function write(person) { alert("Thank you for conacting us, "+person+"!"); } function formgo() { var name=document.getElementById('namein').value; if (name==null || name=="") { alert("Please enter your name"); return false; } writety(name); document.forms('contact').reset(); } </script> </head> <body> <form> <fieldset id="contactinfo"> <legend>Contact Details</legend> <label> Name <input type="text" name="name" id="namein" placeholder="First and Last name" /> </label> </fieldset> <fieldset id="submitbox"> <input type="submit" id="submit" value="Submit" onclick='formgo()' /> </fieldset> </form> </body> please reply or email me thank you -misstam Hi I'm making a php/html page and using javascript to keep a count for me. Code: <script type="text/javascript"> var i=0; }</script> . . . <form action="index.php" method="GET"> <input name="game_button" id="guess1" type = "submit" name="1" value="1" style="height: 50; width: 50; font-size: 16" onClick = "i=i+1;"/> </form> <input type="button" onClick = "alert(i);"/> The last button is just a test button, which will tell me what the value of "i" is, but I'm not getting anything when I cllick that button. However if I change the onClick from alert(i), to alert('i') it will alert me a string, so I know the javascript is working. Any ideas? Hi Guy's, Hope I'm posting in right forum I haven't been here for years I have a program that allows us to insert an alert box on our website triggered by the no right click....... Now we just insert text because the program allows us to but I want to use "&" this is going into a Javascript and it comes out as "&" I know JS uses ampersand I have tried all methods of putting this in but just get & etc...... Any chance someone knows otherwise plz?? Frank Hi, Can anyone please tell me that " how to write a JavaScript function to pop up an alert() if a user scrolls down the webpage faster than a certain speed" Looking forward for a urgent reply Thanks I'm writing a calculator script that will need user input (how much they pay on internet, phone or cable bill) and then multiply those numbers by a known rate and then show the customer the new number. I have the math setup (working right now), and the script works to spit out the answers in an alert. Instead of the alert (which is the only thing I can do correctly at this point), I need the script to give the answers for each bill and then a total answer at the bottom. The script is here, it's all in the index file. http://gotsmith.com/calculator/ Thanks. Hell All Can some1 give me a function for Alert Visitor before the page Load if he /she Sure wants to See the Page . I have function Onclick alert message but i want the function is when click and go to the page and before the page load to ask 'if want realy to see the page' if yes return true if cancel or no to go back to where he come from. Thank you and hope such function exist. currently working on a project and I cant figure the right coding to get my results to pop up in a new page, same window AND an alert box. I got the alert box part down perfectly but cannot figure out how to get my results. Even if i can figure out how to get them on the same page in their designated field, I can probably figure the rest out, the coding i have so far looks like this: ( i cant figure out how to make it look cool in a diff. window... sorry) <HTML> <HEAD> <b> Here is a pay slip generator that will determine your net annual salary. <br> Simply type in your first and last name along with gross salary and hit calculate. <br> Doing this will let you know how much you will end up paying in both Federal and State taxes. </b> <form action="" method="post"> <p> Enter your First Name: <input name="num1" type="text" id="num1" size="10" maxlength="10"> </p> <p> Enter your Last Name: <input name="num2" type="text" id="num2" size="10" maxlength="10"> </p> <p> Enter your gross salary: <input name="num3" type="#" id="num3" size="10" maxlength="10"> </p> Click this button to calulate your annual net pay! <script type="text/javascript"> function calculate() { var fName = document.getElementById('num1').value; var lname = document.getElementById('num2').value; var sal = Number(document.getElementById('num3').value); alert('Hello '+ fName + ' '+ lname + ', you would pay $'+ sal * .2 + ' in Federal taxes and $'+ sal * .1 + ' in State taxes, leaving you $'+ sal * .7 + ' to take home annually!'); //for testing } </script> <input type="button" onclick="calculate()" value="Generate Pay Slip" /> <input type="button" value="Reset Form" onClick="this.form.reset()" /> <p>Total federal taxes: <input name="num4" type="#" id="num4" size="10" maxlength="10" value= N/A> </p> <p>Total State taxes: <input name="num5" type="#" id="num5" size="10" maxlength="10" value= N/A> </p> <p>Net annual salary: <input name="num6" type="#" id="num6" size="10" maxlength="10" value= N/A> <script type="text/javascript"> function show_results { var ftax = document.getElementbyId('num4').value; var stax = document.getElementbyId('num5').value; var npay = document.getElementbyId('num6').value; } </TEXTAREA> </FORM> </body> </html> </script> Any clues would be very helpful What i am trying to do is to make a system on my website that will grab Data from one or two different divs on one page and have it post it in an alert box on the homepage. http://killerz297.webs.com/events.html on that page i would like to be able to have javascript automatically grab the text from the description and paste it into the red box on this page: http://killerz297.webs.com/index.html what i have so far is on this page in the red box i have a script that countsdown how long until the event is away from the set time i put in it until the event starts at a set time it shows the event description, and then while the event is going on it says event in session, then after the event is over it says event no longer ongoing. but then i have to edit the script to the next event. I would like javascript, or what ever type of code it would take to do this to automatically grab the information. I would like for it to automatically switch the information to the next line when the current event is over and start a countdown from there. This is a lot of coding to do but i just am not THAT familiar with how to write javascript to do this myself. One other thing i would likkeeeee is for the red box on home page to disappear until it is 24 hours until next event, and then automatically close 1 hour after event gets over. Not a big deal if i can't get this but i would love it if someone could. okay finally, that is about it lol. If someone could help me with this i would appreciate it soo soo much! Thanks, hope someone could help me. I have been looking all over the web for a solution, because my website displays funky in safari. To fix this i figured i'd tell users that if they are using safari, then using javascript, i could tell them a message. Originally i tried detecting safari then displaying a message. But i couldn't ever find a browser detection for safari. So i thought wait.... If i could: 1. get a javascript alert saying my message. 2. time that message alert frequency by a session, say 10 days? or even every browser session... Then i could solve my problem. However i can't figure out any of this. So if anybody would be willing to help, i'd be very grateful. Hi, I have a client who has downloadable PDFs on several pages of their site and they want an alert message to come up the very first time they attempt a download from their site. I have found the "alert once message script" in the Javascript Kit, however, the alert message is applicable to each page that has the script on it. Can it be coded so that the alert message only shows on the very first download, no matter which page this is on? Code is as follows: Code: <script> //Alert message once script- By JavaScript Kit //Credit notice must stay intact for use //Visit http://javascriptkit.com for this script //specify message to alert var alertmessage="Welcome to JavaScript Kit! \n http://javascriptkit.com" ///No editing required beyond here///// //Alert only once per browser session (0=no, 1=yes) var once_per_session=1 function get_cookie(Name) { var search = Name + "=" var returnvalue = ""; if (document.cookie.length > 0) { offset = document.cookie.indexOf(search) if (offset != -1) { // if cookie exists offset += search.length // set index of beginning of value end = document.cookie.indexOf(";", offset); // set index of end of cookie value if (end == -1) end = document.cookie.length; returnvalue=unescape(document.cookie.substring(offset, end)) } } return returnvalue; } function alertornot(){ if (get_cookie('alerted')==''){ loadalert() document.cookie="alerted=yes" } } function loadalert(){ alert(alertmessage) } if (once_per_session==0) loadalert() else alertornot() </script> Thanks, Wizard247 is there a way in HTML or Javascript to open a new tab (or new window) and force it to be a new tab (or new window) after alert confirmation of javascript? onclick="window.open('google.com', '_blank'); return false;" OR document.location.href = "google.com"+"&target='_blank'"; not working Code: <?php if ($_POST[oke]) { ?> <script language="JavaScript">alert('thanks'); document.location='google.com' </script> <?php } ?> <form target="_self" method="post"> <input type="submit" name="oke" value="save"> </form> how to put the target _blank Hi, I need to a small task in Javascripts and I would like to know whether this can be done. If someone knows a way that can be done or provide me some code, I would be more greatful. This is the task I need to do :- I have a small shoppig cart. I want when a customer visit the site, if that is his first time on the website and before he quit the browser or go to another URL of another domain or press back and go to another page, an alert should pop up. I have did this using cookies and Javascripts and it is working properly as I wanted. But this alert pops up even when I go inside of my own web. I want this alert only to popup when they leave from my web or go to anyother and it should not popup when I click on my links or go in to my web. I hope, I explained what I want and it is clear. Please if someone knows a solution for this. Please send me your ideas or if you have some codes that can be helpfull to me, please send me. Thank you. |