You should be using some kind of custom made popups and dialogue like
http://umairj.com/27/how-to-create-simple-modal-dialogue-using-jquery/
http://www.jquery4u.com/windows/14-jquery-modal-dialog-boxes/
Videos
I have this function to ask for a basic password and then load a php script:
function passwd(){
var password = prompt('Enter the password');
if(password.toLowerCase() == "password"){
window.open("./files/restart-calibre.php")
}else{
alert("incorrect password!!");
}
}
As I am very new to this all of it is copied from stackoverflow.
Now this code does open the script in a new page which I do not want. What would be the best way to achieve this?
Your code is a little bit weird so it's hard to make the adjustment properly but this is gist of it:
showNewWindow: function(menu) {
var me = this,
newWindowId = sports.util.Utils.randomString(12);
//
// Make a synchronous request so that the new window will
// not load as a popup.
//
debugger;
var popup = sports.util.Utils.openNewWindow('', 'menu', {}, null, null, newWindowId);
Ext.Ajax.request({
async: false,
url: sports.util.Utils.getContextPath() + '/tabClicks.do',
params: {
oldWindowId: sports.util.Utils.getWindowName(),
newWindowId: newWindowId
},
success: function() {
popup.location.href = "/desktop/main";
},
scope: me
});
},
Popup blockers try to tell when a window is being opened in direct response to a user action or spontaneously by the application. The way they probably do this is by checking whether the function that called window.open() was run in response to a user-triggered event like a mouse click.
When you perform a synchronous AJAX request, the function that was triggered by the mouse click is still running when the response arrives and the success function calls window.open. So it's considered to be a user-requested window, not a popup.
When you use asynchronous AJAX, the click handler is no longer running when the success function runs. The asynchronous call to window.open is considered spontaneous by the browser, so it blocks it.
I don't think there's any way around this, because anything you could do could also be used by everyone else to get around popup blockers, and they would be useless.
Basically, you'd use a modal instead of close/open. See the lightning:overlayLibrary component for methods you can use to display and close a modal.
Great answer sfdcfox, using modals is definitively the better way to go!
In addition, for some cases, the window methods can be still useful:
window.open()andwindow.location()display a confirmation dialog to ask the user if they would like to navigate away from the current domain. This is in an effort to prevent phishing attacks, e.g. salesforce.com -> saelsforce.com.window.close()should work as expected, just remember that scripts may not close windows that were not opened by them.
I hope this helps!
There are two ways to solve the problem: Note: "window.opener" is not supported by IE if "showModalDialog" is been used.
1) Instead of "window.showModalDialog" use "window.open"
2) If you want to use "window.showModalDialog" then do the following:
<script language="javascript" type="text/javascript">
function YourFunction()
{
var opener = null;
if (window.dialogArguments) // Internet Explorer supports window.dialogArguments
{
opener = window.dialogArguments;
}
else // Firefox, Safari, Google Chrome and Opera supports window.opener
{
if (window.opener)
{
opener = window.opener;
}
}
// write you code and refer "opener"
window.close();
}
</script>
You can pass arguments to showModalDialog function. Simply pass window object as an argument.
window.showModalDialog(theURL, window);
Yo can access the arguments from the modal window using dialogArguments. See: http://msdn.microsoft.com/en-us/library/ms533723%28VS.85%29.aspx
var openerWindow = window.dialogArguments;
The window.open() call now supports the feature "noopener".
So calling window.open('https://www.your.url','_blank','noopener') should open the new window/tab with a null window.opener.
I'm having trouble finding a reliable list of supporting browsers (and versions) - MDN states here that
This is supported in modern browsers including Chrome, and Firefox 52+.
From my experimentation, I see it works for:
- Chrome 61
- FireFox 56
- Safari 11.1 (thanks Jiayi Hu for this)
But doesn't work for:
- IE 11.608
- Edge 40
(All tests on a PC running Windows 10...)
For backwards compatibility it may be better to combine this with t3__rry's answer.
Use
const yourWindow = window.open();
yourWindow.opener = null;
yourWindow.location = "http://someurl.here";
Credit goes to Mathias Bynens: https://mathiasbynens.github.io/rel-noopener/
On the same page?
location.href = this.value;
<td>
<form>
<select name="menu1" id="menu1">
<option selected="selected" value="">...</option>
<option value="ListArchivedProjects">List archived projects</option>
<option value="ListArchivedCertificates">List archived certificates</option>
<option value="SelectProject">List Projects</option>
<option value="ListCertificates">List Certificates</option>
<option value="AddProjectForm.jsp">+ Add new project</option>
<option value="AddCertificateForm.jsp">+ Add new certificate</option>
</select>
<script type="text/javascript">
var urlmenu.onchange = function() {
window.open( this.options[ this.selectedIndex ].value, '_self');
};
</script>
</form>
</td>
change it to
var winMain = window.opener || window;
It says is there is no window.opener, use window.
A window.open equivalent is below
var w = window.open('', '', 'width=400,height=400,resizeable,scrollbars');
w.document.write('Content goes here');
w.document.close();
Use it according to your need.