views:

661

answers:

2

I am attempting to enhance a internal web application we use. I am using a custom favelet to autopopulate certain fields. The javascript variables I need to populate a form exist on a different HTML page internally. Is it possible to access vars from another html page? They would be embedded in the external pages document element.

I do not want to open a popup or redirect the page.

This will be embedded into the page as a favelet.

Thanks!

+1  A: 

You can use an iframe and access its variables via the frames object in an onload handler:

<script type="text/javascript">
    // This function will run as a callback when the iframe loads
    function frameLoad() {
       // Access the foo variable from external.html
       var foo = frames["externalFrame"].foo;
    }
    // Create an invisible iframe
    var iframe = document.createElement('iframe');
    iframe.width = 0;
    iframe.height = 0;
    iframe.style.display = "none";
    // Reference the page on the same domain with external variables on it
    iframe.src = "external.html"
    // Give it a name and ID so we can access it via the frames object
    iframe.id = "externalFrame";
    iframe.name = "externalFrame";
    // Set the load handler
    iframe.onload = frameLoad;
    // Append the iframe to the DOM
    document.body.appendChild(iframe);
</script>
James Wheare
The javascript in my original answer had some errors that I've now corrected.
James Wheare
Good to know. I had a similar problem only recently, where my app was "jailed" in an iframe but I had to access variables outside the frame. I didn't know that this is possible at all.
Matthias
Thanks for the info! I think this will work but I am getting some errors. Look below.
Cody N
Problem is unfort I never make it to the WWALoad function because a document.write occurs for some reason. I am unable to append any text to the page without it writing [object]. Better explination here http://stackoverflow.com/questions/1353215/javascript-dynamically-add-frame-to-framesetSorry still trying to get the hang of stackoverflow. Posting in correct places. Thanks for all the help!
Cody N
A: 

iFrame looks like it will work, however I still am having some problems in relation to accessing the vars ... this flushes the current window displaying [object]?

Here's what I got so far ...

javascript:
var issueDoc = window.frames.editor.document;

function wwaLoad() {
    alert(issueDoc.control.getSortedIdList());
}

var listFrame = issueDoc.createElement('iframe');
listFrame.width = 0;
listFrame.height = 0;
listFrame.style.display = "none";
listFrame.src = "lp.jps?data=1";
listFrame.id = "WWALIST";
listFrame.name = "WWALIST";
listFrame.onload = wwaLoad;
issueDoc.body.appendChild(listFrame);
Cody N
Your `lp.jps?data=1` frame window context should be accessed with `window.frames['WWALIST']` rather than `window.frames.editor.document`
James Wheare
Problem is unfort I never make it to the WWALoad function because a document.write occurs for some reason. I am unable to append any text to the page without it writing [object].Better explination here http://stackoverflow.com/questions/1353215/javascript-appendchild-is-causing-document-write-favelet
Cody N