Reputation: 6490
I found a java script online which redirect a page if is open outside of the frame of index page. This works when all pages are in main diectory, but when the page is one directory higher then it doesn't work.
When I use the location page as: index.html then is the page is open in new tab it doesn't redirect.
If I use ../index.html then everytime I try to click on the link to the page it refreshes my index page.
Any ideas?
<!DOCTYPE html>
<html>
<head>
<script language="javascript">
var framespage="index.html"
if (top.location==document.location)
{
top.location=framespage;
}
else
{
var parent_location=parent.location.href;
var str_beginning=parent_location.length-framespage.length;
if (parent_location.substring(str_beginning, parent_location.length)!=framespage)
{
parent.location=framespage;
}
}
</script>
<link href="../styles.css" rel="stylesheet" type="text/css" />
</head>
</html>
EDIT
I found a piece of code that does the job.
My other question is how to delay loading page? I want to inform a user that user not supose to load pages outside of the frame.
How to delay page redirecting?
<script language="javascript">
if (top.location == self.location)
{
top.location = '../index.html'
}
</script>
Upvotes: 1
Views: 147
Reputation: 6490
I did some searching and I came across code which does the job:
<script language="javascript">
if (top.location == self.location)
{
top.location = '../index.html'
}
</script>
Upvotes: 1
Reputation: 2751
So essentially, the script will set parent.location = framespage
if they are not the same, but the issue you're having is when your index.html
file is in a separate directory.
You have a few options:
index.html
to its absolute path (e.g. http://www.mywebsite.com/index.html
)Determine where you are in the directory structure and modify the framespage
variable.
var path = document.location.pathname;
if (path.indexOf('/') !== -1) {
framespage = '/index.html';
} else { framespage = 'index.html'; }
The more that I think about it, why not just add a front slash so that it always goes to the root. Can you elaborate?
Upvotes: 1