How to open new HTML tab after clicking on the button - javascript

I have the landing page with some buttons to function.
When I click the Send Money button, it opens the sendMoney.html in the same tab. The code is following,
$("#sendMoney").click(function () {
var address = $('#address').find(":selected").text();
location.href = 'sendMoney.html#' + address;
});
How to open the sendMoney.html in the new tab?

To do what you require you can use window.open(). Although the name suggest it'll open a popup window, the request will actually be redirected to a new tab in all modern browsers. Try this:
$("#sendMoney").click(function () {
var address = $('#address').find(":selected").text();
window.open('sendMoney.html#' + address);
});

$("#sendMoney").click(function () {
var address = $('#address').find(":selected").text();
window.open('sendMoney.html#' + address);
});
Instead of using window.location, use window.open("your link");. With window.location its not possible to do that.

You can use <a> button text here <a/> to make a button and use attribute target as _blank to open page in new tab
Or
you can use window.open() function as
window.open(URL, name,)
Value for name can be
blank - URL is loaded into a new window. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded name - The name of the window (Note: the name does not specify the title of the new
window)

You need to use window.open() which will open new tab in most of the new browsers. Here is the updated code:
$("#sendMoney").click(function () {
var address = $('#address option:selected').text();
window.open('sendMoney.html#' + address);
});

Related

How to add target=“_blank” to location.href using MVC? [duplicate]

I am using this bit of jQuery code to get href of the link:
var url = $(this).attr('href');
-- and this bit of code to go to that href:
window.location = url;
Everything is just the way I want it, except the new page opens in the same window as the previous one, and I want it to open in a new window or tab (something that in plain html would have been achieved by using target="_blank" formula).
Question: How can I open the href in the new window or tab with jQuery?
Thank you for your help!
You need to open a new window:
window.open(url);
https://developer.mozilla.org/en-US/docs/DOM/window.open
Use,
var url = $(this).attr('href');
window.open(url, '_blank');
Update:the href is better off being retrieved with prop since it will return the full url and it's slightly faster.
var url = $(this).prop('href');
Question: How can I open the href in the new window or tab with
jQuery?
var url = $(this).attr('href').attr('target','_blank');
The .ready function is used to insert the attribute once the page has finished loading.
$(document).ready(function() {
$("class name or id a.your class name").attr({"target" : "_blank"})
})
Detect if a target attribute was used and contains "_blank". For mobile devices that don't like "_blank", this is a reliable alternative.
$('.someSelector').bind('touchend click', function() {
var url = $('a', this).prop('href');
var target = $('a', this).prop('target');
if(url) {
// # open in new window if "_blank" used
if(target == '_blank') {
window.open(url, target);
} else {
window.location = url;
}
}
});
If you want to create the popup window through jQuery then you'll need to use a plugin. This one seems like it will do what you want:
http://rip747.github.com/popupwindow/
Alternately, you can always use JavaScript's window.open function.
Note that with either approach, the new window must be opened in response to user input/action (so for instance, a click on a link or button). Otherwise the browser's popup blocker will just block the popup.
Try using the following code.
$(document).ready(function(){
$("a[#href^='http']").attr('target','_blank');
});
url='/controller/action';
window.open(location.origin+url,'_blank');

How to detect url change in a new browser tab and get back the url?

I have a link that will open a url in a new tab like this:
<span data-bind="text: connectorLoginLink, visible: shouldShowConnectorLoginLink, click: openTeamViewerUrl"></span>
As I want to detect the URL change in the new tab, I thought I had to use window.open(url) to open this link. Then, keep track of the new window.
In viewmodel:
public openTeamViewerUrl() {
var newWindow = window.open('http://www.google.com', '_blank', 'location=yes');
}
I want to set the opened tab to a newWindow object, then monitor it by using something like this:
var currentPage = newWindow.location.href;
setInterval(function () {
console.log(currentPage);
if (currentPage !== newWindow.location.href) {
// page has changed, set new page as 'current'
currentPage = newWindow.location.href;
console.log(currentPage);
// do other thing...
}
}, 1000);
I got the error back when trying window.open: Blocked opening 'http://www.google.com/' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.
Really got stuck on this. How to by pass the allow-popups? Can this feature be achieved using other methods?
To detect a change of URL you can use javascript unload event on the window, see https://developer.mozilla.org/en-US/docs/Web/Events/unload
window.addEventListener('unload', function(event) {
console.log('Navigation occuring');
});

Open link in new tab when user clicks

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");
})

How to stop a custom bookmarklet from opening multiple browser tabs

I use a standard bookmarklet that looks something like this:
javascript:(function () {var p = document.title;var uri=document.location;window.location = 'http://localhost:8084/'})()
However, every time I use it, it generates a new tab. How do I stop window.location from opening a new tab, or better, how do I get it to load the page in another tab if it exists (ie, if localhost is already open, that's the tab that will be used.)
This question looks similar to
open url in new tab or reuse existing one whenever possible
window.open(URL,name,specs,replace)
URL : Optional. Specifies the URL of the page to open. If no URL is
specified, a new window with about:blank is opened
name : Optional. Specifies the target attribute or the name of the
window. The following values are supported:
_blank - URL is loaded into a new window. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded
name - The name of the window (Note: the name does not specify the
title of the new window)
< script >
document.getElementById("container").onclick = function(evt) {
if (evt.target.tagName === "A")
window.open(evt.target.href, evt.target.href);
return false;
} < /script>
<div id="container">
goo
sta
</div>

Allow user to change URL in window.open()

I have the following code which opens a new window when a user clicks on a button:
$('.loginButton').click(function () {
var strString = $("#medlineSearch").val()
var strSearch = "http://google.com&query=";
var url = strSearch + strString;
alert(strSearch+strString);
var windowName = $(this).attr('id');
window.open(url, windowName, "height=750,width=1250,scrollbars=1,resizable=1,location=1");
// custom handling here
return false;
});
What I am looking to do is allow the user to enter something else in the address bar which seems to be read only.
Is there any way to change that?
If you want to be able to change it, set the target to _blank.
This will prevent the new page to open as popup, but as new page.
window.open
( 'http://google.com'
, '_blank'
);
The jsfiddle.
You cannot change the url in popup window.
Read change url of already opened popup
Trick to do so
open console and type location.href='http://link.com'

Categories