Check condition and automatically redirect to a link, html
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.
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.
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.
Sure. Do it through javascript:
window.location.href = 'http://www.google.com'
From within a tag you can use something like this
<tag onclick="if(condition == true) window.location.href = 'http://mylocation.com/'>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.
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
.
.
}
?>
精彩评论