Opening an alert outside of the popup window in a Chrome Extension - javascript

I have a Chrome extension I am writing where I have a button on the popup page, that when pressed should open an alert with a little help text for the user. I can get the alert to appear in the popup window by calling:
var button = document.getElementById("HelpMe");
button.addEvenListener("click", function(){
alert("HelpText");
});
But this opens the alert in the popup window, which puts it mostly offscreen. I want to make the alert appear in the main tab, which would put the alert right in the center of the screen.
I tried this:
var button = document.getElementById("HelpMe");
button.addEventListener("click", function(){
chrome.tabs.executeScript(null, {
code:`
alert("HelpText");
`
});
});
But it didn't seem to do anything, and I can't get any error messages or logs to appear that I have been able to find. Advice is appreciated.

Related

Close window after print

I have a page that when it loads, I want it to open the print dialog, and then when the print dialog is closed, the window needs to close. I have tried many things. I currently have:
<script type="text/javascript">
window.onload = PrintMe;
window.onafterprint = function () { window.close() };
function PrintMe() {
window.print();
}
</script>
In Edge, this seems to be working fine. I chrome, it's working some of the time, which leads me to believe that it's a timing issue. Sometimes the page will load and the show the print box, and close when the print box closes. Sometimes, the page closes before it loads. Sometimes the page not close after the print box is closed. Any ideas?

windows onbeforeunload doesn't work Javascript

I want to prevent a close tab or close browser in a web page.
I am using this code:
<script type="text/javascript">
window.onbeforeunload = function(e) { //Doesn't work well
return 'Exit';
};
$('#form').submit(function() { //No alert on submit form (this works)
window.onbeforeunload = null;
});
</script>
If I click somewhere in the page the code works fine and ask me if I really want to close the page, but if I reload the page and then I close the tab without click something in the page the tab close without show any alert.
Do you have some idea how to fix?

How to open popup window with window.open() having the confirmation message as Yex/No?

I am opening a pop-up window having the Message asking for confirmation that "Do you want to delete a record" with Yes/No buttons. But, its not working correctly. Below is the code:
var modal = window.open (sUrl,"", "dialogHeight:" + iHeight + "px; dialogWidth:" + iWidth + "px;status=no;scrollbars=yes;resizable=no;titlebar=no;", null);
modal.dialogArguments = messageObject;
As the program goes to this line it opens the pop-up window but does not load it and proceeds further finishing the whole JavaScript code. I want the program to stop till it gets confirmation from the child window Yes/No.
Note: I am getting the value of Yes/No using a hidden variable in the parent window. Here is the code written in child window and which gets me the confirmation value:
window.opener.setMsgValue(returnValue);
This is the function written in the parent window:
function setMsgValue(msgSel) {
document.getElementById('msgSel').value = msgSel;
}
I know I can do this using the window.showModelessDialog. But, this works only in IE8 and I am doing this code for chrome 39.
You could use something like
if (confirm("Are you sure?")) {
// Delete it!
} else {
// Do nothing!
}
It's shorter, but every browser styles it different.

How to prevent closing a dialog created by showModalDialog()?

In normal cases, it is possible to handle the unbeforeunload event to present a message to the user which gives him the final choice wheter to close or not, as shown here:
<script>
function closeIt() {
return "Any string value here forces a dialog box to appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
But this seems not to work in markup that is shown by window.showModalDialog.
Update: That means if this script is used in the HTML-Code which is displayed by the dialog to prevent the dialog beeing closed accidentially.
No message appears and the window closes if the "X" is clicked.
I managed to show a dialog using the <body onunload="return confirm('Really?')" thing, but in either way the window closes.
I was testing on Internet Explorer 9.
Any ideas?

MSIE window.open not opening

So I'm having a popup that opens upon click on a link, but IE (8.0) doesnt want to show it.
Send page
And this function in JS:
function tellafriend(url) {
popup = window.open(url, "Send page", "width=500,height=600,scrollbars=yes,resizable=yes");
popup.focus();
}
IE tells me the error is at the line popup = window.open...
Any ideas on how to fix it?
I can't tell you why this happens, but IE doesn't let you have a space in the second parameter of window.open. If you change it to
popup = window.open(url, "Sendpage", "width=500,height=600,scrollbars=yes,resizable=yes");
the window will pop up.

Categories