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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rude links in IFRAME 1

Status
Not open for further replies.

12Monkeys

Technical User
Joined
Aug 2, 2003
Messages
9
Location
US
Hey-
I am coding a navigation page for accessing a number of intranet pages (content which I don't own, but need to display) and many of these pages have links with the _parent target set.

This wouldn't be a problem, but I have the content displayed in an IFRAME, and when the user clicks those "Rude Links" it makes the navigation frame disappear, and loads the page at the TOP location.

I would like to write a script that does any of the following:

A) prevents / overrides the target designation of all links for the external page in the iframe

B) intercepts the unload event, determines if there was a _parent reference, and if so ignore it

C) using nested iframes, the Javascript verifies that there is an iframe within an iframe, and if not, dynamically adds an iframe back to the page

This is kind of the reverse of all the questions I see where people want to avoid their pages being loaded by outside sources.

Your help is really appreciated,
-12Monkeys

 
Are the pages contained in the IFRAMES on the same domain as your site?

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Yes. Almost all of our internal websites have the same [company].com domain format, but where I'm struggling is that the included content is owned by another group and I can't affect the code inside those pages.

I was just looking at the code that the included pages use for their links, and it's in the form of a hyperlinks with onclick="parent.location='URL';"

I also tried an onunload event for my container page that executes this function:
top.location.href = "my frames page URL" but this causes some strange effects - you can't use the address bar in the browser to go to another location, and bookmarks / links are also disabled. (I think there was a problem with closing the browser too, but I'm not 100% sure).

My code needs some intelligence built into it to decide if the container page is loaded at the top, and if not, to allow whatever the action was. I'm not concerned about people stealing my content because we're behind the firewall.
 
>>I also tried an onunload event for my container page that executes this function:
top.location.href = "my frames page URL"


OK, I think you might be onto something. To simply put top.location.href = "my frames page URL" into the onunload event handler will kill any navigation. However, in your code you can inspect event.srcElement which will tell you which (if any) element triggered the unload event. If the unload came from within the frame, then you can return false and load the location into the correct frame. Otherwise, if it came from the address bar, favourites or your navigation frame, you can proceed as normal.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Yes! I think that is the best way. (Somebody on an alternate forum said I could use an array of hyperlinks to redefine what they do, but I have a suspicion that with all of the safety that is built into the browser, you can't just alter hyperlinks of a document in an iframe from the page that contains it.)

Any idea what the code would look like that you woud need to use to examine and qualify the event.srcElement?

:-) I think you have really caught the idea, so the the main thing would be whether having this occur within a frameset makes the code more difficult.
 
>> you can't just alter hyperlinks of a document in an iframe from the page that contains it

You can (if they're on the same domain), and that was my original thought too, but soon saw that you have no way of knowing when a new page has been loaded into the iframe. Hence you might be able to do it for the first page loaded into the iframe, but how would you know when to trigger the search & replace on subsequent pages.

Unfortunately the onunload event doesn't support the srcElement property for some reason either, so it appears I've given you a bit of a bum steer there too. Let me do some digging and I'll try and figure out a workable solution.


Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
OK, this is a hack. It relies on you not using window.location on your page otherwise you'll break your own code as well.

Simply redefine the location object on your container page, such that parent.location = xyz from the page in the iframe has no effect.

Add:
[tt]<script>
function location(){
}
</script>

To the <head> of your container document.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Ok, but I'm still thinking this can be done, if the srcEvent is used correctly.

O'Reilly JavaScript & DTHM Cookbook lists the following code for determining which element received the most recent event:

function myFunction(evt){
evt = (evt) ? evt : ((window.event) ? event : null);
if (evt) {
var elem = &quot;(evt.target) ? evt.target :
((evt.srcElement) ? evt.srcElement : null);
if (elem) {
// perform event processing here
}
}
}

But this looks like a lot of gibberish to me. First of all, the book doesn't really explain what it does. Maybe if I'd read the previous 8 chapters, chapter 9 would make more sense, but I'm in a bit of a hurry. Plus, even if I did understand it, I wouldn't have a clear idea how to write code for the event processing.

I can't beleive I'm the first person wo ever wanted to do this, but I haven't seen any other posts asking the same thing.

Does this generate any new ideas?
(I really appreciate the earlier responses, BTW.)
 
Sorry, Dwarfthrower, I didn't mean to ignore your post. Is that all the code I need to accomplish it? Seems kinda short (maybe too easy). How does it work? Just blocks attempts to change the location?
 
Okay! It works! Disreagrd my previous post. I don't care how it works (well, maybe a little). The key is: it accomplishes the block of those links without a reload, and without defeating the address bar / favorites & links. Dwarfthrower, you rock!
 
Good to hear... It works by overwriting what the browser knows as 'location'. The location object is available as part of the browser's document object model. Objects in JavaScript are defined as functions, so declaring a function - even an empty one - with the same name as an existing object will override the natural behaviour of the intrinsic object.

Probably not a 'good' thing to do on a regular basis, but effective in this instance.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Sweet.
That really solved my main problem.

Anything I can do about links that are set up with a good old-fashioned HTML target like &quot;_TOP&quot;?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top