Forum Moderators: open
If the url in the address bar is page.htm
{ redirect to page.php }
I don't think a simple redirect would work because the php page includes the html page, so it would include the redirect to itself.
if(window.location!= "page.htm"){
window.location = "page.php"
}
So I tried this
if(window.location!= "www.mysite.com/page.htm"){
window.location = "www.mysite.com/page.php"
}
and I get this wrong url:
[mysite.com...]
if (parent.name = page.htm){
window.location = "page.php" }
That works?!
1. page.htm isn't quoted, so it will be considered a ref to the htm property of a nonexistent object, page.
2. There is only a single = operator in the if( ). That's not a comparison, it's an assignment. It will always eveluate to true, that's if it's assigned to anything sensible!
3. The window name is totally separate from it's location. Usually windows only have names if they are explicitly given them. eg
open("mypage.htm","pop1", "_features_")
pop1 will be the name of the new window.
If you are testing: window.name!= "page.htm", then it will almost certainly always be true.
Oh yes. really, you should be using either:
a) window.location.href = "page.php"
b) window.location.replace("page.php")
The second one means that the new page replaces the old one in the history, so it's no longer reachable via the back button.
/* redirectToPHP:
* Redirects page if extension is not ".php"
*
* use:
* a) Specify redirect URL (relative¦¦absolute):
* redirectToPHP("apage.php")
* b) If no URL is specified
* the page will be redirected to the same
* address, but with the extension swapped to ".php":
* redirectToPHP()
*/
function redirectToPHP(URL)
{
var loc = location.href;
var iLastDot = loc.lastIndexOf(".");
var ext = loc.substr(iLastDot);if(ext == ".php") return;
URL = URL ¦¦ loc.substr(0,iLastDot) + ".php";
location.replace(URL);
}// nb: change ¦¦ to unbroken pipes.