JavaScript redirect

by 7 replies
8
So I want to send people to a page that will eventualy redirect to them to the content they want, I am having a problem because i want the code to take a variable from the url, for example the site I want it to redirext to would be specified in the original url, for example:

PHP Code:
www.site.com/file.php?page=www.redirectedsite.com 
Redirected site being the new url...

At first i tried this, combining some php

PHP Code:
<html>
<head>
<script type="text/javascript">
function delayedRedirect(){
    window.location = "<?php echo $_GET['page']; ?>"
}
</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>You'll be redirected soon!</h2>
</body>
</html>
If I replace the red bit with a normal url such as Google It will redirect no problem but with the php It takes the url but gives me www.site.com/http://www.google.com

I have tried to explain the best I can, any help would be wonderful

Thanks In advance!

I have also tried this but it dosent even pick up the redirect url...

PHP Code:
<html>
<
head>
<
script type="text/javascript">
var 
link getUrlVars()["page"];
function 
delayedRedirect(){
    
window.location link
}
</script>
</head>
<body onLoad="setTimeout('delayedRedirect()', 3000)">
<h2>You'll be redirected soon!</h2>
</body>
</html> 
#programming #javascript #redirect
  • By "red" bit I meant the entire PHP statment, I cant seem to edit my above post :S
  • You don't need to use Javascript to do the redirect. Or is it just that you wanted the timer function to work as well?

    Just do it all with PHP (note that I prefixed the location with http:// since your example only had www.)



    However, if you must use JS, you're super close! It's probably that you're just too close to the problem to see the solution

    Again, it's that a proper URL is not just www, you need the http://. That's why the JS code thinks the new location is an internal page, not a new domain.



    You could also use the PHP parse_url() function to check if the "page" var is a real URL.
    http://php.net/manual/en/function.parse-url.php
    • [1] reply
    • Thankyou, dam I was close, I had tried HTTP$_Get. anyway thankyou for the help, I need javascript for the delayed redirect
      • [1] reply
  • Ah forgot about that slight issue..

Next Topics on Trending Feed

  • 8

    So I want to send people to a page that will eventualy redirect to them to the content they want, I am having a problem because i want the code to take a variable from the url, for example the site I want it to redirext to would be specified in the original url, for example: PHP Code: www.site.com/file.php?page=www.redirectedsite.com