Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevent individual page access 3

Status
Not open for further replies.

adamroof

Programmer
Joined
Nov 5, 2003
Messages
1,107
Location
US
Hey all, i have a small iframe with a src attr of a page somewhere else. How can i stop that page from being loaded by itself, like someone typing in the url, and if they do, it will load the main document with the iframe itself?

lemme break this down..

Content
<iframe src=" />
Other stuff


if type in - it should redirect to

Heres what ive done, but doesnt quite work (redirect works, but then the iframe wigs out and reloads over and over and over and over)

page.html
js
body onload
if (parent.location.href=document.location.href) {
//if top url = this url then redirect
parent.location.href=' }
else
{
//must be in the iframe where i want it
label.value = document.write(parent.location.href);
}


thank for any help here.
 
On the main page set a global variable like:
var isMain = true;

In the page.html check for that value and if it is not set true then redirect.

I do not know for certain that this will work, I never deal with iframes and do not know if a global variable in the main document is available within the iframe. If it is, this should do it.

If the global variable is not available to the iframe then perhaps in your main page when you set the location for page.html you can also pass a parameter like:
<iframe src=" />

And within the page.html you parse the querystring to see if the parameter was passed and if it was not you do the redirect.

If you are on an Apache server a third option would be to use a .HTAccess file to redirect any requests for page.html to index.html.



It's hard to think outside the box when I'm trapped in a cubicle.
 
I don't remember the exact code, but it goes something like this. Put this in the FRAMED page (page.html):
Code:
if ( top.location.href == window.location.href )
   window.location.href = "[URL unfurl="true"]http://index.html";[/URL]
Bascially it says "if this is the top window, then load the other page instead."

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Lots of ways then... I use
Code:
if (document.location == parent.location){
    document.location.replace("[URL unfurl="true"]http://index.html");[/URL]
}
which seems to work.
 
sorry took so long, but glad i waited, mp9's is what i used in the end.

is that not what i tried above tho? maybe the " == " was needed instead of the " = " ?

i thot == was for variables and = was for non variables.
 
= is an assignment operator. You assign a value or object to a variable by doing var1 = var2.
== is a logical operator and works as a comparator instead of an assignment.
So
if (var1 == var2)
compares the values
while
var1 = var2
assigns var2's value to var1.

I mess up with these all the time though and only type in a single = in my if statements and it takes a while to figure out where I went wrong. It's a problem jumping back and forth between VBScript and Javascript. :)


Google, you're my hero!
 
theniteowl: I have the same problem! It can create some really frustrating bugs. Worse, I also use VB a lot, so I have three languages to get confused.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top