0

When below code is executed in chrome, firefox, no error comes. But safari throws TypeError: Attempting to change configurable attribute of unconfigurable property. I don't understand if src is inconfigurable then why don't error comes for chrome and firefox.

Object.defineProperty(iframe, 'src', {
    writable: false, configurable: true
});

Safari Version 11.0.1
PS - I know changing the existing non-configurable property throws this error but then why not chrome and firefox

megha
  • 41
  • 1
  • 7
  • It's only non-configurable in Safari. Different browsers have different DOM implementations. – SLaks May 07 '18 at 16:55
  • 2
    Don't try to change property attributes on host objects. Why are you trying to do this? – Bergi May 07 '18 at 16:55
  • @Bergi I have set my src = "" and I don't want my src to be modified otherwise it causes cross origin error. – megha May 07 '18 at 16:57
  • @SLaks Is there any way out? – megha May 07 '18 at 16:57
  • @megha What would modify the `src` attribute? – Bergi May 07 '18 at 17:00
  • 1
    @megha - *"...and I don't want my src to be modified..."* So...don't modify it? This sounds like an [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) where the above is Y; maybe we can help with X? – T.J. Crowder May 07 '18 at 17:00
  • @Bergi @ T.J. Crowder I run my script on third party, so it can be unintentionally modified by them. – megha May 07 '18 at 17:09
  • @megha If your script runs on a third party page, you are at their mercy regardless what you do. You have to trust them to run your code as you expect. – Bergi May 07 '18 at 17:15

1 Answers1

2

I don't understand if src is inconfigurable then why don't error comes for chrome and firefox

Because different browsers implement the DOM in different ways. It would appear that Chrome and Firefox don't mark src non-configurable. Safari apparently does. Also note that on Chrome (at least), the src property is inherited and has a getter/setter, so calling defineProperty on an iframe instance will create a new property on that instance, rather than modifying the behavior of the one formerly inherited from the prototype. (I didn't check Firefox or Safari.)

You'll need to address your underlying requirement in a different way if you want to be compatible with Safari.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875