Skip to content
Advertisement

How to prevent “my own” javascript code to open a new window?

This one is hard

I have this task to build a module for a big system made with PHP. The most important rule is that I can’t touch the code of the core system, so I can’t fix some of it’s bugs.

The main service of my module is to create a custom document that is not edited by the system default editor, but by a custom form created by my module.

To prevent users from editing the custom document instead of filling the form, I can set the document to blocked at the time of creation.

But even with the document being blocked, the system opens its default editor in a new pop-up window, and then shows an incorrect error message that has nothing to do with the block, and it is messing with the users heads.

I could fix this in a minute by changing a few lines in the core code, but then I would loose my job. I also can’t ask the owner of the system to fix the problem because it would take forever (the owner is from a public institution).

The system function goes like this:

  1. When user creates document, it passes execution to a function on each module, so these modules could have the possibility to react to the document creation;
  2. The module receives data about the document created and checks if its type is the custom document type, then block it if true;
  3. The module passes execution back to code system, which finishes creating document (so I can’t use die() otherwise document isn’t created at all);
  4. The system open an editor so user can put content in it.

So I came with an idea that my module could, after blocking the document, output a javascript tag with code that would prevent javascript code created by the core system from using the window.open() command, or at least hide this pop-up.

Is there a way to do it with javascript?

Advertisement

Answer

You can overwrite the open if your code can run before the open occurs:

const myOpen = window.open;
let iwanttopen = false:
window.open = function() {
  if (iwanttoopen) return myOpen(...arguments); // here YOU decide
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement