Can javascript "listeners" working across frames?
October 15, 2009 3:59 PM   Subscribe

Guerilla-coder Javascript filter: Rather than wait 12-18 months to get my project idea into my company's queue, I'm trying to find a workaround. It's going to be ugly. It's going to be messy. And I have no idea how to do it. Bottom line: My company's web portal has a top frame that I can control, and a bottom frame controlled by Web IT. Can I add an event listener to the top frame that will watch the bottom frame and excecute when it's done loading?

So, I work for a major healthcare insurance company. The project queue is 12-18 months for major stuff and 9-12 months for piddly stuff. Naturally, we make exceptions for breakfixes (and for execs who have a pet project and scream alot, of course), but even simple stuff takes forever.

As an example, I put in a request a year ago to have a simple piece of javascript loaded into the web portal; it's just getting put in, this weekend.

Believe it or not, I really do like my job. It's almost become something of a game to see how much I can do with the few parts of the webpage I can control. Anyway ...

The top frame of the web portal is fully under my control, but never refreshes. As a result, I can't use addLoadEvent() or any of my usual tricks. Is there some sort of listener that can sit in the top frame and watch the bottom frame?

I can give lots more details if needed (no, this is not me going rogue; yes, I have approval to dick around in our test environments to see if this is going to work; yes I'm really trying to do something that will benefit our users). Please keep in mind my skill level is only intermediate, so I may have to do lots of research ... but I'm more than willing to do that. I just need a direction to go.
posted by jpolchlopek to Computers & Internet (14 answers total) 1 user marked this as a favorite
Are both frames in the same domain?
posted by Artw at 4:06 PM on October 15, 2009

Two questions:

First, do you need this code available for anyone else? If not, GreaseMonkey isn't a bad way to get your code running on some other site.

Second, are the two frames loaded from the same domain? If so, yes, you can totally go muck around in the DOM of the other frame. Either window.parent or window.defaultView.parent should have the master window, then do window.frames[1] for the other frame.

If it's *not* the same site, that's much more difficult. What exactly are you trying to do?
posted by effugas at 4:10 PM on October 15, 2009

You might find what you're looking for in one of these questions at
posted by blue_beetle at 4:10 PM on October 15, 2009

If all three frames are served from the same hostname+port, then this should be possible. (Otherwise, the browser's same-origin policy will stop you, for security reasons.)

Something like this might work:

.addEventListener("load", function() { alert("done"); });

I haven't tested this. There might be some complications - I'm not sure if you can access the contentWindow property of the frame before it's loaded, so you might need to use setInterval() to poll until it's loaded, instead.
posted by mbrubeck at 4:13 PM on October 15, 2009

Wow, that was quick!

effugas: Yeah, this isn't just me dicking around, locally. I need to have the javascript run for all users of the portal.

What I am trying to do: Since this is a healthcare web portal, people are checking their benefits, etc. However, the lovely deutsche-bachs in Sales frequently sell what I like to refer to as "pink elephants" -- incredibly non-standard cases that don't work with our current benefits system.

Yes, guys, that's awesome that you landed that client, ... but we don't actually have an entry in the benefits system for a Penile Implant benefit ... what's that you say? You already promised the client? Nice work. (bastard)"

Here's the setup: Most of the time, the pages in the bottom frame have a few areas (portlets) that I can control -- I deploy content to a database and the IBM Portal Builder software retrieves it; but only for certain areas on certain pages.

But on this particular page, there are no portlets under my control (otherwise I use my javascript, there). But I still have the top frame!

So, I need to watch the bottom frame for a particular elementID, then run my javascript ... that way I can write in "Penile Implant 100% coverage" or whatever.

mbrubeck: The network diagram for the portal is a goddamn cat's cradle, but it all goes to the same java front-end. I think that shouldn't be a problem.

Thanks, again, everyone.
posted by jpolchlopek at 4:33 PM on October 15, 2009

While mbrubeck posed a very workable answer, I have to think that jQuery could simplify this greatly... I'll look into it and post back if I come up with something.
posted by ElDiabloConQueso at 4:52 PM on October 15, 2009

I'd Nth using JQuery. Very easy to learn, and simplifies javascript coding immensely. The only reason I'd ever advise against it is if some other Javascript library is in use already... so you might want to see if scary sounding intranet portal thing comes with that. Just to be clear, scary sounding intranet portal is generating the frameset and all of the frames, but you have a way into it to insert arbitary code (if only in one of the frames)?
posted by Artw at 4:59 PM on October 15, 2009

Artw: Yeah, I can deploy content to a) some frames on most pages, b) the top frame at all times, or c) the top frame only for some pages. This is option C, I'm afraid.

I inline whatever I need into the top frame as soon as the portal loads, I suppose, but I'm still not sure how to write a listener, JQuery or not.

Also: is it really scary-sounding, or am I missing a joke? :)
posted by jpolchlopek at 5:08 PM on October 15, 2009

JQuery is ridiculously well documented, and it sounds like all this stuff is hosted from the same domain so you can go DOM traversing. Be sure to at least use basic try/catch though, because someone might change something and it may come to your code manipulating their DOM.
posted by effugas at 8:33 PM on October 15, 2009

"Also: is it really scary-sounding, or am I missing a joke? :)"

I don't want to put words in his mouth, but "I'm responsible for content I don't have the power to modify hosted by people who are neither beholden to me nor those I make my commitments to and can't have any of this delegated to my control due to institutional inertia" is a pretty scary situation to be in, roughly of quitting, moving away, and learning ice carving magnitude.
posted by majick at 6:09 AM on October 16, 2009

Welcome to a large healthcare company in Corporate America, kiddies!

It's not really that bad, majick -- I'm not officially responsible for anything other than the portlets I control. This is more of a skunkworks project, than anything else, so that I can expand my field of control.

In other, question-related news, I got JQuery loaded but I can't get the listener to work. I'm practically ready to just do something that just checks for a page ID every second, or so, but that's pretty hacky for even me.
posted by jpolchlopek at 2:09 PM on October 16, 2009

Heh. It's pretty much as majick puts it - as soon as someone mentions something weird like "portlets" my estimate of potential-pain-in-the-ass factor goes up by an order of magnitude.
posted by Artw at 2:12 PM on October 16, 2009

"that's pretty hacky for even me."

I don't mean this as a criticism but what you're trying to do here is already a hack that sounds like it could reach epic proportions if you continue to use it to expand your official or unofficial field of responsibility. I get that you're working around a broken process that has attached itself to a broken technology managed by broken assumptions. Sometimes the only way out of that is to hack. Just keep in mind: at this point you're already talking about crossing the line of near un-maintainability, so from here on out it's just a matter of degrees.

And yeah, just the mere mention of "portlet" is pretty scary. That's the kind of thing you wind up dealing with when people aren't willing to let you just work on a page to fix it, since that would violate their delicate sense of organizational hierarchy or allow you to do work that "should" only be performed by the ancient mummy who is raised once per year just before the systems freeze in December or whatever. A portlet is the kind of thing you encounter when politics are breaking the technology, rather than the more desirable other way 'round.
posted by majick at 6:01 AM on October 18, 2009

…I deploy content to a database and the IBM Portal Builder software retrieves it…

I just wanted to add my condolences.
posted by Civil_Disobedient at 8:26 AM on May 23, 2010

« Older Is there a proper term for thi...   |  What are different cultures' v... Newer »
This thread is closed to new comments.