JavaScript - Onbeforeunload To Redirect User
In my onbeforeunload event, it asked the user whether they want to leave the page or stay on it. When they click "stay on page", I want it to then redirect them to another webpage in the same window. Is there a way to do this, without just showing/hiding divs? Can I override the function? I'm currently trying to do it this way(as seen below) but now the dialog box is not showing up at all. It was working before Any ideas as to what I'm doing wrong, or how to accomplish this task?
Code: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>The Shocking Truth - Cabot Market Letter</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#vid").show(); $("#div2").hide(); }); var test = 1; function manipulatetest() { test = 2; } window.onbeforeunload = onbeforeunload_Handler; function onbeforeunload_Handler() { if (test == 1){ $("#vid").hide(); $("#div2").show(); var confirm = confirm("Would you like to stay on this page?"); if (confirm == true) { window.location = "http://www.google.com"; } } } </script> </head> <style type="text/css"> body { background-color: #e0e6e5; } #vid { margin: 20px auto; width: 920px; } </style> <body> <div id="vid"> <video width="920" height="540" autoplay preload controls> <source src="shockingtruth.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'> <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'> <source src="shockingtruth.ogv" type='video/ogg; codecs="theora, vorbis"'> <object width="920" height="540" type="application/x-shockwave-flash" data="flowplayer-3.2.1.swf"> <param name="movie" value="flowplayer-3.2.7.swf" /> <param name="allowfullscreen" value="true" /> <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> </object> </video> </div> <div id="div2"> <video width="920" height="540" autoplay preload controls> <source src="shockingtruth.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'> <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'> <source src="shockingtruth.ogv" type='video/ogg; codecs="theora, vorbis"'> <object width="920" height="540" type="application/x-shockwave-flash" data="flowplayer-3.2.1.swf"> <param name="movie" value="flowplayer-3.2.7.swf" /> <param name="allowfullscreen" value="true" /> <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> </object> </video> </div> <p style="text-align: center;"><a href="http://www.cabot.net/info/cml/cmlld03.aspx?source=ed01" onclick="manipulatetest();">Click Here To Order</a></p> </body> </html> Similar TutorialsOk, im stuck. Ive tryed about 10 solutions from Google. None are working. Ok, I made a site in Flash, and a second site in HTML. I want to automatically redirect people from my flash site (indexhigh.html) to my html site (indexlow.html) if they can't see my flash movie. Here's the flash page URL: http://testnewsite.netau.net/indexhigh.html . Restrictions: This redirect MUST be done using javascript or without having me to download another swfobject file. Please paste completed code. Heres my current code: Code: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Website</title> <script language="javascript">AC_FL_RunContent = 0;</script> <script src="AC_RunActiveContent.js" language="javascript"></script> </head> <body bgcolor="#ffffff"> <!--url's used in the movie--> <!--text used in the movie--> <!-- saved from url=(0013)about:internet --> <script language="javascript"> if (AC_FL_RunContent == 0) { alert("This page requires AC_RunActiveContent.js."); } else { AC_FL_RunContent( 'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0', 'width', '940', 'height', '940', 'src', 'Website', 'quality', 'high', 'pluginspage', 'http://www.macromedia.com/go/getflashplayer', 'align', 'middle', 'play', 'true', 'loop', 'true', 'scale', 'showall', 'wmode', 'window', 'devicefont', 'false', 'id', 'Website', 'bgcolor', '#ffffff', 'name', 'Website', 'menu', 'true', 'allowFullScreen', 'false', 'allowScriptAccess','sameDomain', 'movie', 'Website', 'salign', '' ); //end AC code } </script> <noscript> <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="940" height="940" id="Website" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="false" /> <param name="movie" value="Website.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /> <embed src="Website.swf" quality="high" bgcolor="#ffffff" width="940" height="940" name="Website" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object> </noscript> <table width="889" border="2" cellspacing="0" cellpadding="0"> <tr> <td width="91" bgcolor="#FFFFFF">Site created by:</td> <td width="232"><a href="http://www.christoff.tk"><img src="my_Logo.png" width="197" height="169"></a></td> <td width="242"><a href="http://www.fcw.comoj.com"><img src="logo3.JPG" width="205" height="62"><br> Free Charity Websites </a></td> <td width="112">And by the</td> <td width="198"><p><img src="untitled.png" width="77" height="86"><br> Fort Myers High School <br> Web Design 2 Students from<br> 1st Period<br> Team leader: Chris Christoff </p></td> </tr> </table> </body> </html> Chris Hi, I rarely use javascript so pardon my newbieness. I'm trying to create a function that is triggered by a <select> onchange event. It's supposed to save the newly selected value in a variable, then refer the user to a link location + variable. This is what I tried: Code: <script type="text/javascript"> function categoryRef() { var a = this.options[this.selectedIndex].value; document.location = 'recommend.php?cat=' + a; } </script> <select name="category" onChange="categoryRef()"> <?php $getCategoryQuery = "SELECT category FROM categories ORDER BY category"; $ConnectQuery = $database->query($getCategoryQuery); while($category = mysql_fetch_array($ConnectQuery)){ if($getCategory == $category['category']){ echo "<option selected='selected'>" . $category['category'] . "</option>"; } else{ echo "<option>" . $category['category'] . "</option>"; } } ?> </select> When I try this and change option nothing happens. Any pointers? I need the code for if the user enters in a code into the field for example E1 and clicks submit it'll redirect them to a certain form. And if another user entered in E2 it would redirect them to a DIFFERENT form. I hope this is the correct category of the site for me to post this. Wasn't sure if it should be here or PHP.
Hi, Please i am not getting clear understanding of a javascript code to redirect user to a different web page based on the day of the week. Please i have search for many script and not getting clear understanding from their code.I have also try several code myself but nothing good has come from it. Please can someone help me and write a descriptive code for me to understand better.I really plead you and your to help me...but i belief the Lord God will bless you for your time helping me. looking forward to here from you. Thank you. Clement Osei Hello! Is there any way to disable this annoying event? (I'm using IE) Code: <html> <body onbeforeunload="alert('bye');"> </body> </html> I tried to put this at the address bar: Code: javascript: document.body.onbeforeunload=''; But it didn't worker either... Anyone has an idea? Thanks! I set some value like in onbeforeUnload document.getElementById('hGridStateModified').value='YES'; The value is not set and is not available at server side. ( I set some value like document.getElementById('hGridStateModified').value='YES'; The value is available at server side. ) Code /// START f(window.body){ window.body.onbeforeunload = saveBeforeExit; // IE else }else{ window.onbeforeunload = saveBeforeExit; // FX } function saveBeforeExit() { show_confirm(); } function show_confirm() { var result=confirm("Do you want to save the changes before navigating."); if (result==true) { document.getElementById('hGridStateModified').value='YES'; return true; } else { document.getElementById('hGridStateModified').value='NO'; return true; } } ///END Thanks in advance. Best Regards, Sandip Hi, I have the following problem: When I use this script it pops up both if a user tries to close the window and also if he presses the back button. Please tell how to show another window or do another action when the back button is pressed? I know it is possible although a lot of people say it's not. Code: <script type="text/javascript" type="text/javascript"> window.onbeforeunload = confirmExit; function confirmExit() { return "blabla"; } </script> This is a bit of a strange one. I have been trying to call a function in a child object from the parent object but the child seems to be going out of scope in onbeforeunload function. These function calls work outside of a onbeforeunload, so it only fails when called in onbeforeunload. I can fix it by making the child object a global but I was trying to avoid that. Anyone know why my childobject is out of scope when called in onbeforeunload? Notice I call that same function in windows onload event and it works fine. Here is the code (I have simplified as much as possible to just show the error): Code: <html> <head> <title>Broken Page</title> <script language="javascript" type="text/javascript"> var myparent; function windowLaunch() { myparent = new parent(); myparent.getchildvalue(); window.onbeforeunload = myparent.getchildvalue; } function parent() { this.mychild = new childobject("myinput"); this.getchildvalue = function() { var tmpval = this.mychild.returnvalue(); }; } function childobject(thename) { this.myprop = thename; this.returnvalue = function() { return (document.getElementById(this.myprop).value); }; } </script> </head> <body id="thebody" onload="windowLaunch();"> <div id="outerdiv"> <span title="This Input Box">My Input:</span><br /> <input id="myinput" style="width: 290px"/> </div> </body> </html> i dont want to show the confirm message when using window.onbeforeunload. Please suggest a solution to hide the confirm message.. -------------------------------------------------------------------------------- I am currently using onbeforeunload to show user a message 'click ok to save, cancel to continue' Question.. On browser close event (user clicks on window close button 'X' on browser), I do not want to show the message to user. Can you please help with how to check the browser close event. Other details.. If user closes the browser, my ajax request is not getting executed and hence I am not able to save the data to database. Hence I am planning not to show the popup when user closes the browser. Thanks in advance... Sandip I am creating a popup for an online store so that when prospective customers put something in their shopping cart and then try to leave the site, they are offered a coupon in a popup. I am using the following code (FYI, uses prototype.js) Code: Event.observe(document.body, 'click', function(event) { if (Event.element(event).tagName == 'A' || Event.element(event).tagName == 'INPUT' || Event.element(event).tagName == 'IMG') { staying_in_site = true; } }); window.onbeforeunload = popup; function popup() { if (staying_in_site) { return; } if (popup_displayed) { return; } popup_displayed = true; //popup displayed here } The popup is not displayed when the user clicks on a link or submits a form (correct behavior) and it is displayed when the user types in a URL or tries to close the browser (also correct behavior). The problem is when the user clicks the back or forward buttons, the popup is displayed which is not the desired behavior. Anyone here have a solution for this? is it possible to capture the control.event or element.event that was fired to invoke the onbeforeunload event. for example, if a button is clicked and it causes the onbeforeunload event to fire can i determine which button was clicked. thanks i'm a total noob with java and can't seem to figure this out. i want to add a bit on to the current url : this is the code i'm using , it just keeps adding... Code: window.location=(window.location+"?p=62"); am i even doing this right.. any help appreciated.. Hey all im new to the forum as i have just started playing round with javascript but anyway i have this code PHP Code: setTimeout(top.location='profilec.php?id=' + id,4000); and when executed the time delay is not taken into account at all and completly ignored. If i remove the id var from the code and just have PHP Code: setTimeout(top.location='profilec.php?id=',4000); its fine can anyone tell me why this is happening and if there is a solution :-) thanks before hand mike Hi, I'm trying to redirect my users to their profile, but can't figure out how to put the function into the link. sethighlightedValue(); is a function that returns the users username. Is there anyway else to do this? Here's what I got: Code: case RETURN: window.location = "profile.php?user='setHighlightedValue();'"; this.setHighlightedValue(); bubble = false; break; Hi, I'm trying to change a piece of code I found in a scipt: Code: if (errors == null) { jQuery('#requestform') .hide() .html('<div class="thankyou"><h1>Merci</h1><p>Nous reviendrons vers vous.</p></div>') .show(); } Instead of the .html () I would like to redirect to another webpage of my website, in the same window. Anyone can help me? Thanks. what wrong, it just adds the website to the end of the address bar, and it doesnt redirect???help please <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div> <h1>hello</h1> <form onsubmit="return go();"> <select name="website" id="website"> <option value="http://www.google.com/">google.com</option> <option value="www.yahoo.com">yahoo.com</option> </select> <script type="text/javascript"> function go() { var s = getElementById("website"); window.location = s.options[s.selectedIndex].value; return false; //prevents the form from being submitted to the server. } </script> <input type="submit"> </form> </div> </body> </html> I have data pages in my e-commerce site (from 1999) that are imbedded in a parent page using the object tag. I have started to include a little JS snippet that is intended to redirect to the parent if the embedded pages are loaded separately: Code: <script language="JavaScript" type="text/javascript"> try { if (top == self) {top.location.href='parentpage.htm'; } } catch(er) { } </script> This works fine in Firefox, but in IE 7 and 8 it gets into an infinite loop of reloading the parent page into the object frame. Here is a sample: http://www.greathallminis.com/Ancien...s_anc15_mm.htm (I've left this page with the issue, while I have excluded the script for IE elsewhere.) Is there an alternative and equally simple script for IE, or can I modify this to work for IE? Thanks, Rob Hello, I am new on forum and javascript. I have got a query for dynamic geo location javascript. I would like to make a page which redirect to another page based on user REGION. Ex. If user from EMEA then page1.html, if user from APAC then page2.html, if user from Other region then page3.html Hope someone will share their ideas with an example to achive this. Thanks Hello everybody, What I have been trying to incorporate into my site is that if a user has javascript turned off, it redirects them to another page. This is what I have found but doesn't seem to work. Code: <NOSCRIPT> <meta http-equiv=”refresh” content=”0; URL=AlternatePage.cfm”> </NOSCRIPT> When I test it out it shows on the page like this: meta http-equiv=”refresh” content=”0; URL=AlternatePage.cfm” Basically shows the same thing without the tags. It doesn't take any action at all. Any ideas, tips, or advice? I don't have any access to the root directory so SSI is not an option at the moment. (Do plan of hosting it on a dedicated server when I have the money ) |