How to open link in new Tab by pressing Button? in jade - javascript

The following
button(type="button", target="_blank", onclick="location.href='auth/google';")
does not work. It opens link in the same window. Just for reference, its part of node.js program, in which i'm using passportjs for google authentication.

The button isn't actually opening a link - it's just running some javascript code that happens to, in this instance, be navigating to a new URL. So the target="_blank" attribute on the button won't help.
Instead, you need to use javascript commands to open a new tab/window, rather than using javascript to change the URL of the current window. Assigning to location.href will only change the URL of the current window.
Use the window.open(url, target) function instead - it takes a URL, and a target window name, which behaves the same as the target="whatever" attribute on a link.
window.open('auth/google', '_blank');
Your complete code would look like this:
button(type="button", onclick="window.open('auth/google', '_blank');")

Related

I want to click an element using css selectors and open it in a new browser window then send it behind the existing browser window using Jquery or JS

I'm a beginner in Jquery or Javascript so, this is what should be happening.
In a website(not mine nor have a control), I visited I want to click this html element below.
HTML
<a class="_ng0lj" href="http://example.com" rel="nofollow me noopener noreferrer" target="_blank">www.example.com</a>
This link should be opened in a new tab.
When the tab pops-up, will send that window BEHIND the rest of the browser windows.
Where I am:
For now, I am currently in the process of trying to get the link from the class=_ng0lj and store it on a var so that I could use this for the next function.
NEXT:
Execute a function to open this var with the link to a new browser window and send it to the rest of the browser windows.
Jquery
$('div a._ng0lj').click(function(){});
I did open the link but not on the new window plus I want to get the link for the other purposes that's why I want to know how I could grab the link and store it in a var.
Hope this makes any sense.
To get the link : $('a._ng0lf').attr('href')
To open a window in background :
var otherWindow = window.open('about:blank', '_blank');
otherWindow.blur();
window.focus();
otherWindow.location.href = href;

How to use window.location.replace in jquery or javascript

I have a website for which I have implemented livechat, when user clicks on livechat link it opens a new window that is fine. I want to do is when no operator is online then it should not open a new window when livechat link clicked instead it should redirect to the contact us page of the website in the same window in the currently opened tab, Is it possible?
I do not know how is it possible, what I have tried is this , it redirects to this page but opens in a new window.
window.location.replace('http://mywebsite.com/index.php?route=information/contact');
The problem is somewhere in the code around that. You need to call that code in the old window.
If you want to call it in the chat window, try:
window.opener.location.replace('http://mywebsite.com/index.php?route=information/contact');
window.close()
Use the href property instead:
window.location.href = 'http://mywebsite.com/index.php?route=information/contact';
You can use simple javascript code like this: window.location.href = "SOME URL"
it will do the same think. .

open multiple webpages in same tab

Have multiple hyperlinks in a report, everytime a click on a hyperlink the webpage opens in a new tab,
Is it possible to open the hyperlinks in only one tab instead of multiple tabs using javascript ?
Please help
I am currently using window.open to open the webpages, I cannot use target. Below is the code :
I basically have a jqgrid where all the values of a column have hyperlink,
if (GridColNum == 2) //index of the column
{
localStorage.valuekey = $('#filters_grid').jqGrid('getCell', GridRowId, 1);
window.open('http://mywebpage.html');
}
And i am using the clicked value in another page using localstorage feature
Yes, it's possible. Use syntax
<a href="..." target="rptTab"/>
for all relevant links. This way the first time a link is clicked a new tab will be opened; for subsequent clicks, that tab will be reused.
EDIT: if you're opening the link in javascript using window.open, then you need to specify the name of the tab/window as the second parameter:
window.open('http://mywebpage.html', 'rptTab');
You probably have a target='_blank' attribute in your links. Just remove them. If that does not help please post some code so we can see what is happening
When you specify the target of a window.open() call or an tag, you can specify _blank, _parent, _self or _top, as special tab/window names. Alternatively you can specify a new tab/window name, when you specify this tab/window another time, it will refer to the original one that you opened.
hyperlinks opens a new browser tab - is due to
target='_blank'
attribute of it. you can remove it using following jQuery code.
$('a').removeAttr('target');
now on-wards every link will get open in the same page.
You don't need javascript for this. Just use the target-attribute of the a tag. All links with the same target should be opened in the same tab.
http://www.w3schools.com/tags/att_a_target.asp

How to put a window opened with window.open() behind the opener window?

I have a link that when clicked replaces the current page with another through the default <a> element click behaviour, but which also calls a JavaScript function to open another page in a new window. I would like the new window opened from my JavaScript function to appear behind the current window but can't figure out how to do this. In actual usage, I am feeding click thrus to a database, the link that is controlled by the window.open statement. The other link is to the clients site in a new window. I want the clients site to appear on top.
My current code is as follows:
<script language="JavaScript" type="text/JavaScript">
function countClicks(a,b)
{
window.open("http://stackoverflow.com?id="+a+"&id2="+b, "_blank");
}
</script>
test
So for the example URLs shown above I would like the (original) window with Google to appear in front of the new window with StackOverflow, but the new window always opens in front.
Try using window.focus() at the end of your existing function:
function countClicks(a,b) {
window.open("http://stackoverflow.com?id="+a+"&id2="+b, "_blank");
window.focus();
}
That should bring the existing window back to the front, noting that behaviour may vary between browsers and depending on popup-blocking settings, etc.
Or you could just swap the two urls, i.e., put the StackOverflow url in the href with the appropriate query string specified directly rather than in JS, and put the Google url in the window.open() call.
Popup behavior varies based on browser/user settings. I haven't been able to replicate the open behind behaviour myself, but I believe what you're looking for is .focus().
Try:
window.open("http://stackoverflow.com?id="+a+"&id2="+b, "_blank").focus();

window.location.href vs clicking on an Anchor

What's the difference between clicking on:
<a href />
vs.
calling window.location.href = ...
?
Wherever possible, you should use <a href="foo.html"> over window.location.href, for a number of very good reasons.
If you have javascript disabled, none of the links would work.
Spiders, such as Google Bot, do not interpret javascript, and so they won't follow any of your links.
IT BREAKS THE INTERNET. No, really though - the World Wide Web is built on the very basis of discoverable linkages between pages. Hiding these linkages with non-standard .. err, links, goes against that very premise.
It makes for a bad user experience: a user expects that when they mouse over a link, they will have access to some information:
the destination displayed in the status bar (very important!)
right-click -> copy link location
middle-click -> open new tab
etc
Using window.location breaks all of these
It's much easier!
Setting window.location.href = 'thepage.html' is the same as calling:
window.open('thepage.html', '_self');
I.e. the target is limited to the same window, as that is where the location property is. This has the same effect as clicking a link without a target attribute:
...
You can use the open method instead to specify a different target, like a new window:
window.open('thepage.html', '_blank');
This has the same effect as clicking a link with that target:
...
You can also use the open method to open a new window. The return value is a reference to the window, so you can use that to set the location of that window instead of the current window:
var w = window.open('about:blank', '_blank');
w.location.href = 'thepage.html';
Don't forget that in addition to the above answers, clicking on a hyperlink (anchor tag) will trigger that element's onclick handler (if any), whereas the Javascript version clearly doesn't and just changes the window's location.
It is possible to manually invoke the onclick handler from Javascript if you want to simulate a click, but you must remember to do this manually. The snippets you posted would differ in this regard, which could be the cause of any behavioural differences.
With the anchor you can specify the target property, but with window.location.href you can't.
Generally the anchor is used when a user wants to redirect the browser to another location, window.location.href is used when the redirection is done using javascript.
In addition to the other answers given, clicking on an <a> element with the href attribute sapecified will cause the browser to navigate to the URL in the href, regardless of whether JavaScript is enabled or not.
document.referrer contains a reference on the server and client to the url of the page that contained the link the user clicked to get to the new page-
scripted location methods do not.

Categories