aggixx Posted April 28, 2008 Share Posted April 28, 2008 I want to make a 404 page for my website that redirects the visitor back to their previous page, and gives them a message, telling them a 404 occurred. I want the it to give the effect that if they click on a broken link, a message pops up saying 404, and that they didnt even visit the broken page (or something close to that).I've tried: <HEAD> <script LANGUAGE="JavaScript"> <!-- Begin redirTime = "1"; redirURL = windows.history.back(); alert("Error 404: Page Not Found You have been redirected back to your previous page.") function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); } // End --> </script> </head> <BODY onLoad="redirTimer()"> I've also tried:<HEAD> <script LANGUAGE="JavaScript"> <!-- Begin alert("Error 404: Page Not FoundYou have been redirected back to your previous page.")window.history.go(-1)}// End --></script> </head><BODY onLoad="redirTimer()"> Thanks ahead of time.Edit: Oh, I forgot to say how it wasnt working. When you go to a page that doesnt exist, the message pops up, but it doesnt redirect. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 28, 2008 Share Posted April 28, 2008 You might want to use a meta refresh instead of a Javascript redirect. There's plenty of information on Google about using that, it's 1 tag that goes in the head. The page content can say that you are being sent back to the last page. You might want to use a server-side language to go back to the right page (the referrer). Link to comment Share on other sites More sharing options...
aggixx Posted April 28, 2008 Author Share Posted April 28, 2008 Im not a big fan of server-side anything. Is there any way else I could do it? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 28, 2008 Share Posted April 28, 2008 First, take the HTML comments out of the Javascript code and instead of using language="javascript" use type="text/javascript". You can't have linebreaks inside strings in Javascript, you're getting a syntax error. Remove the line break in the call to alert. If you want a line break to appear in the text, use the "\n" sequence. Link to comment Share on other sites More sharing options...
aggixx Posted April 28, 2008 Author Share Posted April 28, 2008 Ok, now neither the alert or redirect work, but heres what I have now: <head><script LANGUAGE="text/javascript">function redirtimer(){alert("Error 404: Page Not Found \n You have been redirected back to your previous page.")window.history.go(-1)}</script></head><BODY onLoad="redirTimer()"> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 28, 2008 Share Posted April 28, 2008 Change the "language" to "type". Link to comment Share on other sites More sharing options...
aggixx Posted April 28, 2008 Author Share Posted April 28, 2008 Now the redirect works, but not the alert. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2008 Share Posted April 29, 2008 You have the code like above right, with alert before the redirect? I thought alert boxes were synchronous, so the alert box should be closed before it executes the next line. You can always have it alert on page load and delay the redirect by a couple seconds. function redirtimer(){alert("Error 404: Page Not Found \n You will be redirected back to your previous page.")setTimeout("window.history.go(-1)", 3000);} Link to comment Share on other sites More sharing options...
aggixx Posted April 29, 2008 Author Share Posted April 29, 2008 Could I just make script that checked if it came from the 404 page and if it did, have the alert pop up? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2008 Share Posted April 29, 2008 You can use document.referrer to check the page that the user came from. Link to comment Share on other sites More sharing options...
aggixx Posted April 29, 2008 Author Share Posted April 29, 2008 Actually, forget what I said about it working, it only works sometimes. Heres the url if you want: HereIm working on the site for someone, sorry if its wierd-ish. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2008 Share Posted April 29, 2008 Oh, I assumed you were only posting part of your code here, not the whole thing. You need to make it a complete document. Add a doctype, <html> tag, etc. It's not enough to just have a head and a body. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.