0

So I got this code for a website using Tampermonkey, and I want it to open a window in the center of the screen after the button is pressed. How can I do that?

(function() {
    'use strict';
    //button
    var button = document.createElement("button");
    button.setAttribute("class", "mdl-button mdl-js-button geofs-f-standard-ui");
    button.setAttribute("id", "Button-toggle");
    button.setAttribute("style", "background:clear");
    button.innerHTML = "test";
    document.getElementsByClassName("geofs-ui-bottom")[0].appendChild(button);
    var buttonToggle = document.getElementById("Button-toggle");
    //Event Listener
    buttonToggle.addEventListener("click", function() {
        buttonToggle.setAttribute("style", "background:green");
        console.log("Button Pressed");
    });
})();
  • You cannot just throw some JS without presenting some HTML having: *1:* the desired custom modal window you want to open; *2:* whatever that *geofs-ui-bottom* is; *3:* `background:clear` what's that?; *4:* read [ask], then [edit] with a [mcve]. – Roko C. Buljan Feb 20 '22 at 13:31
  • Thats the thing, I dont know what im going to add yet. I just want to know how i can add a small HTML page in the middle of the window. – TotallyRealElonMusk Feb 21 '22 at 14:03
  • Not sure what you mean by *"window"*. A DIV "centered modal" element? I cannot see any code for your DIV "window" (is it a DIV window or something else?). Where's the HTML, CSS? How is that window generated? What should go into that window? What if you want multiple buttons and different windows? I believe you can really clarify your question and include more code. As you see currently people are incorrectly thinking you want to perform a `window.open` - just because of lack of details from your side. – Roko C. Buljan Feb 21 '22 at 15:06

1 Answers1

-1

You can open a new window with JavaScript It works on code pen well. https://codepen.io/ash_000001/pen/PoORmyg

You can change the position with this line of code:

const myWin = window.open("", "", "left=700, top=350, width=200, height=100");

function myFunction() {
const myWin = window.open("", "", "left=700, top=350, width=200, height=100");
let x = myWin.screenX;
let y = myWin.screenY;
document.getElementById("ao").innerHTML =
"myWin.screenX= " + x + "<br>myWin.screenY= " + y; 
}
<button onclick="myFunction()">Open Window</button>

<p id="ao"></p>
ash
  • 1,065
  • 1
  • 5
  • 20
  • 1
    That wasn't really what I was hoping for. I may have not been clear enough in my question. As a window, I meant an HTML pop up in the page itself. – TotallyRealElonMusk Feb 20 '22 at 10:26
  • oh for popup you can use alert("Message"); – ash Feb 20 '22 at 10:26
  • Again, thats not what i meant, sorry ! in the image below, the green FMC button below opens the window in the middle. How can i do that? view image: https://cdn.discordapp.com/attachments/771661854619205642/944903317799190608/unknown.png – TotallyRealElonMusk Feb 20 '22 at 10:28
  • i can understand we are not in front of each other but you can ask i will try to help you – ash Feb 20 '22 at 10:29
  • yea sorry, i accidentally pressed enter instead of shift enter... – TotallyRealElonMusk Feb 20 '22 at 10:29
  • is that is a kind of website you are showing – ash Feb 20 '22 at 10:33
  • huh? what do you mean? – TotallyRealElonMusk Feb 20 '22 at 10:46
  • sorry @TotallyRealElonMusk But it's hard for me to understand and answer this because i really thought something else and it' something else. – ash Feb 20 '22 at 11:04
  • The answer seems misinterpreting a sloppily written question. Read the comment to OP's question. Also, stop suggestion the use of unsafe inline `on*` handlers. – Roko C. Buljan Feb 20 '22 at 13:32
  • @RokoC.Buljan I should stop answering now. I think question's in Stack overflow what is the full form of op – ash Feb 20 '22 at 13:34
  • @ash you were trying to be helpful, it's OP's fault for not being more precise. But, if you don't understand a question, ask the OP to provide more context. – Roko C. Buljan Feb 20 '22 at 13:35
  • @RokoC.Buljan yeah that's true. I was trying to be helpful. But, he didn't ask questions correctly. Sir please can you suggest to me something by which I could learn JavaScript and programming languages some tips .... please sir..... – ash Feb 20 '22 at 13:37
  • 1
    Ash, it's hard to give a tip in one sentence. Let your passion guide you, and the subjectively "best suggestion" is — to have many, I mean, really many projects under your belt. – Roko C. Buljan Feb 20 '22 at 13:41
  • yeah, sir projects. I will keep this in mind forever @RokoC.Buljan – ash Feb 20 '22 at 13:43