phpSensei Posted March 12, 2008 Share Posted March 12, 2008 Is there anyway to redirect a user to his previous page he was on using Javascript? I was thinking "window.location = history.go(-1);" but it comes out as an error. Any suggestions? I tried google. Quote Link to comment Share on other sites More sharing options...
GameYin Posted March 12, 2008 Share Posted March 12, 2008 javascript:history.go(-1); <a href="javascript:history.go(-1);">Go back</a> If that doesn't work take out the semicolon. I forget if that is neccessary. Enjoy Quote Link to comment Share on other sites More sharing options...
phpSensei Posted March 12, 2008 Author Share Posted March 12, 2008 Yes but I need to redirect him automatically, without clicking the link. If possible Yin Semicolons aren't necessary but its the proper and more efficient way in Javascript. Quote Link to comment Share on other sites More sharing options...
GameYin Posted March 12, 2008 Share Posted March 12, 2008 You can look at metas. <META http-equiv="refresh" content="5;URL=http://www.wahteversite.com"> 5 is the number of seconds. Set it to 1 to redirect faster. Thanks. Enjoy. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted March 12, 2008 Author Share Posted March 12, 2008 Would this be possible? <META http-equiv="refresh" content="5;URL=javascript:history.go(-1);"> Quote Link to comment Share on other sites More sharing options...
haku Posted March 12, 2008 Share Posted March 12, 2008 I personally prefer this method. Add this to the top of your document (assuming its PHP) <?php header("Refresh: 3; url=" . $_SERVER['HTTP_REFERER']); ?> But if your document isn't php, this wont work without playing with the settings on your server. Semicolons aren't necessary but its the proper and more efficient way in Javascript. Got any links for that? Ive always been under the impression that they're unnecessary code and therefore should be left out. Quote Link to comment Share on other sites More sharing options...
nogray Posted March 12, 2008 Share Posted March 12, 2008 put this in the head section (or anywhere you want) <script language="javascript"> history.go(-1); </script> Quote Link to comment Share on other sites More sharing options...
GameYin Posted March 12, 2008 Share Posted March 12, 2008 I'm going to test the meta right now. Let me go do it... Result:YES. it works. Did my suggestion help ? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted March 12, 2008 Author Share Posted March 12, 2008 Thanks Yin, and the rest of you guys... I used the meta refresh with the history.go(-1) I am using Ajax, I got bored of PHP because it just seemed that I knew everything and done everything with it, so I decided to learn Ajax and I learned it in 1 week. Now I am trying to explore the functions and methods of Javascript to make my Ajax fancier, and use PHP without any page refreshes when integrated with Ajax. Its very cool actually, Its like PHP but without the page refresh and I whole lot more... one thing I don't understand: Effect.SlideDown('element'), I cant get it to work, or maybe I don't know how to use it. Quote Link to comment Share on other sites More sharing options...
haku Posted March 12, 2008 Share Posted March 12, 2008 I think you are misunderstanding how AJAX should be used. It shouldn't be used alone, it should be added over top of fully functioning PHP. Users wont necessarily have javascript turned on, and its sloppy programming to have a site that doesn't function properly if the user has it turned off. A true pro will make their site fully functional with or without javascript. Ajax is great. But its an addition, not a base. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted March 13, 2008 Author Share Posted March 13, 2008 I think you are misunderstanding how AJAX should be used. It shouldn't be used alone, it should be added over top of fully functioning PHP. Users wont necessarily have javascript turned on, and its sloppy programming to have a site that doesn't function properly if the user has it turned off. A true pro will make their site fully functional with or without javascript. Ajax is great. But its an addition, not a base. You know, a simple script can alert the user to check if the javascript is turned on or not. I do not use Ajax as the main porgramming language or as the base. I put it in small/sometimes big places like pagination, comments, chat,...etc PHP is the base programming, Ajax just works behind the scenes to update data without pages refreshing all the time. I don't think I am misunderstanding ajax. Quote Link to comment Share on other sites More sharing options...
haku Posted March 13, 2008 Share Posted March 13, 2008 If you are using it that way, then you are right, you aren't using it wrongly. Although with good programming a message isn't needed to alert the user about javascript being necessary, javascript just isn't necessary in the first place. The only real exceptions would be something like a chat program where it cant be done in php, or at least cant be done reasonably in php. Quote Link to comment Share on other sites More sharing options...
GameYin Posted March 13, 2008 Share Posted March 13, 2008 Also, I just realized this, if you click a link, and it takes you to a page saying that you will be taken to your previous page, you will need -2 I think. Otherwise you will go back to the page w/ the link. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted March 13, 2008 Author Share Posted March 13, 2008 Also, I just realized this, if you click a link, and it takes you to a page saying that you will be taken to your previous page, you will need -2 I think. Otherwise you will go back to the page w/ the link. Oh thanks for that. Topic Solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.