I use this code to open 2 sites in new tabs in browser
$("#mylink").on('click', '#one', function () {
window.open('registration.php', '_blank');
window.open('readme.php', '_blank');
but when I click on link, second link has bet blocked as popup.
How to open 2 sites contemporaneously?
P.S. If this is not possible, how open second link in current browser tab?
Unfortunately it's not possible to "unblock" it as a popup, as far as I know. It would be easier to just give 2 links.
You can use _self as parameter to open in the same window. i.e.
$("#mylink").on('click', '#one', function () {
window.open('registration.php', '_blank');
window.open('readme.php', '_self');
}
Related
I want to create a jQuery script that opens a specific url in a new tab if the user clicks somewhere (no matter where). After that, the user should be able to click anywhere without getting a new tab at every click again.
Unfortunately I have no idea how to create such a script, and therefore I would appreciate all your answears :)
You can use the .one() event like
$(window).one('click', function() {
var url = "http://google.com";
window.open(url, "_blank");
})
_blank in window.open() is used to open the link in a new tab.
DEMO
Refer to following links for documentation on .one() and window.open()
$(window).one('click', function() {
var url = "http://google.com";
window.open(url, "_blank");
})
Is there any easy work around to close the current tab and open a new tab by javascript?
Try below :
window.open('http://www.google.com','_blank');window.setTimeout(function(){this.close();},1000)
you can use location.replace for same goal:
window.location.replace('https://stackoverflow.com/');
https://developer.mozilla.org/en-US/docs/Web/API/Location/replace
window.open(url,'_blank');
this.close();
window.open will open the new tab according to the user browser settings only. Sorry that I didn't find any way to open an url in new tab if the user's browser settings wont allow you to.
In browser settings, set it to allow popups and then try. This will work.
Instead, to disable back button you can use options given in this page
http://www.irt.org/script/311.htm
var win = window.open('/Employees/GetAttendenceDataBySelectedData/?attendDateString=' + $("#AttendDate").val() + ', "_blank');
window.top.close();
From my webpage, I am opening a new page in different tab. When the page in different tab will be loaded, I want to close my webpage i.e. Suppose I am on pageA and I opened pageB using window.open(). Now, when pageB will be opened, I want to close pageA. I tried this jsFiddle -
function onClickBtn()
{
var win = window.open('http://www.google.com','_blank','');
setTimeout(function () {
win.close();
}, 5000);
};
This is my HTML markup -
<input id="btn1" type="button" value="Click me" onclick="onClickBtn()"/>
However, this code is closing pageB ,not PageA.
I have tried a jsFiddle https://jsfiddle.net/gdso9eeg/
Please suggest me a suitable solution.
It is doing what it should do.
You opened the new window with that variable and on using close() with it, it is closing the window which it opened.
If you want to close the parent then open the new page on the parent.
Try this:
In place of _blank put _parent
If page is redirecting on the same page window then window.close will not work due to some browser security for this scenario type we need to use one of the following way.
1.window.history.back();
2.document.referrer
3.Request.UrlReferrer server side
The .close() should follow Javascript's window, not win.
Try
window.close();
Good luck!
EDIT:
Have you checked this post window.close and self.close do not close the window in Chrome ?
"..javascript must not be allowed to close a window that was not opened by that same javascript."
It also offers some workarounds to the issue.
I have a button:
<button>Click</button>
I want click it and open a new window in the same page, I bind the click event an event handle:
$('button').click(function () {
var newurl="http://" + window.location["host"] + ";
window.open(newurl, '_parent');
})
But is always open in new tab or new window, the second parameter I have try _self _top.
I even have try window.location.href(newurl)
So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.
Well that's not possible technically, it also depends on browser tab settings, window settings and third party tab manipulation plugins or addons.
Update
OP has cleared the confusion of all through his comment below his question, this is what you need to set a new url for current window:
$('button').click(function () {
var newurl="http://" + window.location["host"];
window.location.href = newurl; // or simply window.location
})
window.location =newurl; will open a new window replacing the parent
Have you tried this?
<script type="text/javascript">
window.open ('test.htm','_self',false)
</script>
If you mean you want to replace the current page, window.location = newLocation; will do.
If you want a modal popup, try JQuery UI Dialog.
i want to open a window in a new tab, but when i want that opener page to be active, not the new one. How can i do this... Many thanks
my code is something like this :
<script language="javascript">
window.open("http://www.google.ro");
window.opener.location.focus();
</script>
To give focus to the new window (but you don't want that, and it will probably have focus by default):
var newWindow = window.open("http://www.google.ro", '_blank');
newWindow.focus();
I don't think it's possible to steal the focus from the new opened tabs. I didn't find any official statement telling this, but all the articles I found on this subject talk about configuring your browser to open tabs by default without focus.
The only "solution" I could come up with is this one:
window.open("http://google.com", "_blank");
window.alert('Hello there! This is a message that annoys you, the user.');
Note that it is possible to shift focus when opening popups.
Learn more about the window object.
This works for me...
var newWindow = window.open('http://www.google.ro', '_blank', 'height=300,width=300,menubar=0,status=0,toolbar=0', false);
window.focus();
To give back focus to the opener window, try using window.focus(window.name); instead of window.opener.location.focus();