I would like to do the following:
Check address bar, if address is not something redirect to http://example.com else do nothing.
<script type='text/javascript'>
if document.location != http://example.com
window.location = 'http://example.com';
<script>
but it's not redirecting.
Please keep in min that the above code will be used in an echo
tag in php.
EDIT:
I would also like be able to distinguish http://example.com/app/1?someparameters
from http://example.com/app/2?someparameters
So if http://example.com/app/2?someparameters
redirect to http://example.com/app/1
Already tried:
echo "<script type='text/javascript'>
if(window.location.href.toLowerCase().indexOf("http://example.com/app2/..") != -1) {
window.location.href = "http://example.com/apps/1/";
alert("Done");
}
else
alert("OK");
</script>";
Try this
if(window.top.location.href.toLowerCase().indexOf('http://example.com/app/2') != -1)
window.top.location.href = 'http://example.com/app/1';
This should work, but with a number of changes:
1) if
condition must be in parens
2) why do you check document.location, but assign to window.location? You should actually assign to document.location.href
3) the string in condition must be in quotes, of course:
if (document.location.href !== 'http://myhost.com')
document.location.href = 'http://myhost.com'