Forum Moderators: open

Message Too Old, No Replies

JS to detect if wrong page is loaded and redirect

         

kapow

4:56 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a php page that includes an html page. I don't want the htm page to load on its own. Can someone tell me a javascript to do something like this:

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.

korkus2000

5:23 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is like frame breaking scripts. You need to use the window.location property:

if(window.location!= "page.htm"){
window.location = "page.php"
}

Now you will need a full address since it will read the entire URL.

kapow

5:27 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now you will need a full address since it will read the entire URL.

You mean like this?:

if(window.location!= "http://www.mysite.com/page.htm"){
window.location = "http://www.mysite.com/page.php"
}

Will I get a problem iF sometimes there is or is not: http:// OR www

kapow

5:39 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just tried this and I get an infinate loop:

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...]

kapow

6:03 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This seems to work :)

if (parent.name = page.htm){
window.location = "page.php" }

I don't relly know JS so can anyone let me know if the above will create bad problems for any typical browsers?

Bernard Marx

9:02 pm on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

kapow

11:43 am on Sep 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well that scares me. Sounds like I'm doing the wrong thing (like I said, I don't know much about javascript).

Can you show me the complete working script?

py9jmas

12:10 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



You could move page.htm outside of the doument root.
Or use .htaccess to deny access to page.htm.
(Assuming PHP is accessing page.htm as a local file, not over HTTP).

Bernard Marx

12:19 pm on Sep 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have only tested this on strings, not on live pages.
Should be OK.

/* 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.