views:

341

answers:

5

I'm creating a sample website using xhtml with javascript support. Also using php for server side programming. I need to redirect the webpage to some other page from an html page, after checking some condition.What is the best method to implement this. I've done it using header("link"); but since i'm using it inside the tag, it shows up a error. is it possible to redirect to a particular link from within the tag.

A: 

Sure. Do it through javascript:

window.location.href = 'http://www.google.com'

Alex
A: 

try this

<?php 
.
.
//your php code here
.
.
if($condition){
?>

script type='css/javascript'>

window.location.href = 'http://domain.com'

/script>

<?php 
 } else{
.
.
//again your php code here
.
.
}

?>
santosh
+1  A: 

The best way would be using PHP. Javascript solutions only work when… well, when javascript is enabled.

<?php

if($do_redirect) {
  header("location: http://www.google.de");
}

?>

Note that this only works, when there was absolutely no output so far.

Nils Riedemann
A: 

It depends on what the condition is.

If it is something you can test for in PHP, then do it in PHP (as one of the very first things you do, before you start thinking about generating output to the browser):

<?php
    if (condition()) {
        header("Location: http://example.com/foo/bar/baz");
        exit();
    }
?>

If it is something you can test for only in JavaScript then:

<script type="text/javascript">
    if (condition()) {
        location = "http://example.com/foo/bar/baz";
    }
</script>

… keeping in mind the principles of progressive enhancement.

David Dorward
A: 

From within a tag you can use something like this

<tag onclick="if(condition == true) window.location.href = 'http://mylocation.com/'&gt;Tag text</tag>

Most HTML tags support the onclick event, so replace tag with the type of tag you need for your situation.

The condition part can be an inline code to find some value, or some Javascript function you defined earlier.

Veger