Suppressing "permission denied" errors in Javascript

17,523

Wrapping your code in a try-catch block should be able to catch and deal with these errors.

Share:
17,523
Mala
Author by

Mala

|\/| _ | _ | |(_||(_| _ \/\/|_|\/_\ |_| _ _ _|(_)(_)

Updated on July 25, 2022

Comments

  • Mala
    Mala almost 2 years

    I have a JS function that polls for the current url in an iframe, the purpose being to ascertain whether or not the iframe is pointing to the same site as the main document. As such, the code is basically:

    function urlCheck()
    {
      var location = document.getElementById('frameid').contentWindow.location.href;
      if (location)
      {
        // iframe src is currently local
      }
      else
      {
        // iframe src is currently not local
      }
    }
    

    Functionally, this code works perfectly. However, in the error console, every time this function is called and the iframe src is not local I get an error:

    Permission denied for [site1] to get property Location.href from [site 2]

    How can I fix my code to avoid these errors?

    Thanks, Mala