简体   繁体   中英

How to restrict user to access iframe in separate tab?

On my index page there are two links for Login and Signup and one iframe

I have designed both(Login and signup) pages separately Now i want to force both pages to be opened only in iframe not in a separate tab and if someone tries to access directly redirect them to index page. here is my code...

 <!DOCTYPE html> <html> <head> <title></title> </head> <body> <a href="login.php" target="frame">Login</a> <a href="signup.php">Signup</a> <iframe src="" name="frame" frameborder="0"></iframe> </body> </html> 

You can add the following to the head of your HTMl document to make it automatically redirect if it is not loaded inside an iframe:

<script>
window.onload = function(){
   if (top === self) {
     //not inside iframe
     location = 'url';
   }
}
</script>

JSFiddle: http://jsfiddle.net/xjqyrsd7/

I just found the Best way to do it with Javascript..

var myUrl = 'http://localhost/test/index.php';
if(window.top.location.href !== myUrl) {
    window.top.location.href = myUrl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM