Wednesday, November 12, 2008

Restricting access to IFRAME (read-only IFRAME)


This is a complementary post to my original post about creating a read-only iframe. The former post opens a new window outside of the users reach and steals the IFRAME focus. The following code makes use of the SECURITY=”restricted” IFRAME attribute that was introduced in IE6.
Since you can’t set this attribute from script once the IFRAME is on the form you need to remove the original IFRAME and create a new one instead.
The following code tells the tail.

One thing to notice is that the IFRAME will be completely inaccessible. This means that you can not script against the IFRAME at all. If you’re using my IFRAME viewer utility this solution won’t work for you.


function OnCrmPageLoad()
{
RestrictAccessToIFrame("IFRAME_Test");
}

function RestrictAccessToIFrame(iframeId)
{
/* Reference the relevant IFRAME */
var testIframe = document.all[iframeId];
/* get the iframe parent container */
var testIframeParent = testIframe.parentElement;

/* if the IFRAME exist */
if (testIframe)
{
/* Save the Original Iframe HTML */
var testIframeHTML = testIframe.outerHTML;
/* Remove the Original IFRAME */
testIframeParent.removeChild(testIframe);
/* Create a new IFRAME Instead */
testIframe = document.createElement(testIframeHTML);
/* Add an IFRAME restriction attribute to the new IFRAME Element */
testIframe.setAttribute("SECURITY","restricted");
/* IF needed rebuild the IFRAME url */
testIframe.src = "/MicrosoftCRM/_root/homepage.aspx?etc=1";
/* Append the IFRAME to its original parent element */
testIframeParent.appendChild(testIframe);
}
}

OnCrmPageLoad();

1 comment:

jessica said...

It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.
school signs uk