Log in

View Full Version : How to fix incorrect javascript in iframe



mvogel
03-29-2008, 10:43 PM
I have a webpage which contains an iframe (served from the same domain so no security restrictions). I have no ability to change the page in the iframe unless I can write javascript from its parent (the page with the iframe in it).

Whoever wrote the framed page used incorrect dom syntax and makes reference to "parent.document.location" instead of "document.location". When the framed page is in its own window I presume it doesn't matter since it's the top window so parent.document.location and document.location are the same. But when viewed through an iframe, the use of the parent.document... instead of document... is causing null references and errors.

Is there anyway from framing page to write some javascript that corrects the javascript in the framed page. In other words, is there some way to change parent.document.location to document.location via javascript in the parent page?

benslayton
03-30-2008, 05:46 AM
Please post a link to the page on your site that contains the problematic script so we can check it out.

mvogel
03-30-2008, 03:48 PM
Unfortunately, the site is behind a secure login so I can't just give a link. But here is the issue. The page (in the iframe) is calling the following javascript function:


function appendFormDataToURL(url,omit) {
var footer = document.getElementById('div__footer');
var elem, frm = parent.document.forms['footer_actions'];
if (isValEmpty(url)) url = ''
for (var i = 0; i < frm.elements.length; i++) {
elem = frm.elements[i];
if (elem.name.length == 0 || elem.name == omit || elem.name.indexOf('inpt_') == 0) continue;
if (elem.type=='select-one')
url = addParamToURL(url, elem.name, escape(parent.getSelectValue(elem)));
else if (isMultiSelect(elem))
url = addParamToURL(url, elem.name, escape(getMultiSelectValues(elem)));
else if (elem.type=='radio')
url = addParamToURL(url, elem.name, escape(getRadioValue(elem)));
else if (elem.type=='checkbox')
url = addParamToURL(url, elem.name, (elem.checked ? 'T' : 'F'));
else if (elem.type=='text')
url = addParamToURL(url, elem.name, escape(elem.value));
else if (elem.type=='hidden' && elem.name != 'frame')
url = addParamToURL(url, elem.name, escape(elem.value));
}
return url; }

I am getting an error on the line:
frm = parent.document.forms['footer_actions'];

I believe this is because it should be frm=document.forms['footer_actions'];

Is there way (from the page caling the iframe) to effect a change of this function? Can I override the function somehow? Can I somehow get the value of document.forms['footer_actions'] from the iframe into the page calling the iframe so that the parent.document.forms['footer_actions'] now id valid?

Any ideas are appreciated?