Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:43:36+00:00 2026-06-09T09:43:36+00:00

Is there any standard way to know if my script has access to an

  • 0

Is there any standard way to know if my script has access to an iframe‘s document?

I couldn’t find anything in the web so I came up with this:

function checkifr() {
    function errHandler() {
        alert('Error. Try again later.');
    }
    var ifr = $('#ifr')[0]; //the iframe DOM element

    try {
        var d = ifr.contentWindow || ifr.contentDocument;
        if (!d) {
            errHandler();
            return false;
        }

        var b = $('#ifr').contents().find('body');
        //... manipulate iframe content

    } catch(e) {
        errHandler();
    }
}

It works, Firefox throws an error when I try to access the iframe‘s contents and ends up in the catch block. Chrome shows an Unsafe JavaScript attempt to access frame warning in the console but never enters the catch block, it seems to return null for those attempts so the initial if (!d) takes care of it. Opera and IE behave similarly to FF. Edit: With the code above, now Chrome returns an “empty” Window object (with no properties) which do not trigger my if block. Check Esailija‘s answer which works nicely cross-browser!

Here’s the fiddle.

I can’t test the iframe‘s src against window.location.

Justificatory Background: This is because this function is part of a small image upload plugin which I’m developing (using an iframe as target since IE<9 doesn’t support XHR2), and the goal is mainly validating connection error/timeout/etc. The default browser pages for these errors are subject to the same origin police, hence the purpose of this question. I don’t want to send an ajax call to check if the page is available, as I want to validate the submit request itself. jQuery’s .load handler doesn’t fire for errors, jQuery’s .error and the onerror HTML attribute do not work for this. An illustration of the working script is available here, but you may ignore it completely and simply answer the question below.

Note that the above block is solely for illustrating why I can’t use the iframe.src.

I do think that there should an easier/standard way to check an iframe‘s “accessibleness”, but I can’t find anything in the web nor SO except half-solutions which simply check the iframe.src and do not apply for many cases. If my hackish try/catch block is considered a technically “clean” solution, others may re-use it if no better option is found.

So is there any standard or simpler way or jQuery plugin to check if my script has access to an iframe‘s document without comparing the iframe‘s src with the window.location? Without try/catch blocks and errors/warnings in the console if possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:43:38+00:00Added an answer on June 9, 2026 at 9:43 am

    Here’s another option, can’t really do it without try catch.

    Tests http://jsfiddle.net/LHjwZ/11/

    function checkIframe( ifr ) {
        var key = ( +new Date ) + "" + Math.random();
    
        try {
            var global = ifr.contentWindow;
            global[key] = "asd";
            return global[key] === "asd";
        }
        catch( e ) {
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to start foobar.js WSH-script in order that standard Task Manager
I know there is an easy way to move any standard config section to
Is there any standard way of debugging Javascript on a webpage that's being accessed
Is there any standard way to route all Key events from the control A
Is there any standard way of implementing some sort of a write-through buffer for
Is there any safe and standard compliant way to treat a C style array
Is there any easy way to change the color of the UITableViewCellAccessoryCheckmark from standard
Is there any way to perform private key encryption in C#? I know about
I know that one can embed RDFa in XHTML documents. Is there any standard
Is there any standard way (or substitute to Open Session [EntityManager] in View )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.