Is it possible to open a new tab in JS?? I tried googling it but most answers were answered before about years that it was not possible then, so is it possible now?!
Thank in advance :))
another question if possible, can we open to separate Urls using the same tag? I mean to open 2 diff tabs when clicking on one hyperlink
Yeah. You could do this in Javascript using window.open('target.html','mywindow','width=400,height=200') added to the onclick event.
See this for more info.
Opening a new tab in modern browsers is the same as opening a link in a new window by setting the target attribute. With HTML you do this by:
Click me
(See HTML a target attribute)
With javascript you do the same by window.open(url, '_blank'...) but remember that most browsers will block this unless it is done on the onClick event, so opening a new tab automatically after some timer has gone off for example is not a good idea.
You can also use link text if it's a link that should open in a new tab.
If you wanted to open both pages at the same time you could always combine both of the techniques mentioned above.
Double Link
This will open the pages thispage.html and thatpage.html simultaneously.
dont forget to add the title attribute to your link as opening new windows/tabs without warning users is generally frowned upon. Something like;
title="Clicking this link will open two new tabs"
should keep the standardistas off your back.
Also, you may want to separate your onclick event from your html as again munging them all together really isnt best practice. If you are using jquery then assign the onclick event by inserting a small piece of JavaScript at the top of your page as such;
$(function(){
$('#the-link-id').click(function(){
window.open('thatpage.html');
});
);
Related
I am using the code shown in the images below. I have tried many ways like closing login window, before opening a new one. I have also tried many ways like window.open("index.HTML", '_self', false) and window.location.replace("index.HTML") but they are not working at all:
I usually use window.open("http://www.example.com", "_blank")
Also check your pop-up blocker since it will most probably detect this as popup and block it.
If not then use a monkey patch which is create an html anchor tag with target="_blank". Hide it with CSS visibility: hidden, then invoke the click event using JS.
Read more about popups and blocking in this answer. If your window.open event was not triggered by a click then most probably it would be blocked.
What you want to do, if I am not mistaken, is open a new webpage but using the same window (or tab)... To do this, you should modify the window.location property.
For example:
window.location = "new_page.html"
This will take you to new_page.html.
If your current location is: /users/muhammad/index.html
Then the page will take you to: /users/muhammad/new_page.html
Leave a comment if you have any questions!
=)
Edit: The link above doesn't contain any answer that applies to this question. If the person who put it there actually read this question they would have realized this.
What's my best option for opening a link in either a new tab or new window when I only have control over the href attribute of an anchor tag?
We have a homegrown menu navigation system that someone before me built and it sets the href of the menu anchor links based on a url column in a database table. So I know there are much better ways to open a link in a new window than what I'm asking for and I wouldn't normally try this, but for now I just need to do it this way.
I've tried using:
javascript:window.open('http://www.goodwill.org')
That does work for opening the link in a new tab, however it changes the current tab to be a blank page except for the text [object], which obviously defeats the purpose.
You need to ensure that the JS expression in your link does not return a value:
javascript: void(window.open('http://www.goodwill.org'))
If you have access to the html, which you do by either html or js
HTML
<a target="_blank">
I have this link in my left navigation:
dashboard
That javascript opens a link based on the passed parameters.
All works fine, but I would like to be able to use the browser capabilities of opening the links in a tab (when user is using middle click or selects 'Open link in new tag' from right click menu). Though, this is not working for links handled with javascript code.
There are many reasons why this is not the default behaviour of the browser (e.g. javascript function might only do some validation and stay in the page ... browser can't know what the js might do or if a new window/dialog will result from that action so would make no sense to open new tag as a result of a middle click ...). But hopefully there is a workaround for the default behaviour.
Any idea how this could be done?
Cheers,
Stef.
Javascript links execute in context of the page where they are called. If you "open" the link in a new tab/window, the javascript code will be executed in the new window, i.e., empty, and will most probably fail.
A browser could try to add the feature you are asking for by cloning the page which contains the link, and executing the javascript code in the context of the cloned page. But this would most likely break some critical sites (imagine for example that your online banking site works with javascript, so when you open a link in a new tab/window, cloning the original window might lead to a duplicate transaction).
At the moment to get to an external link from our intranet, we have to copy the link, and paste the link into a new window. Is there any way we can achieve this in a single function in javascript?
Thanks
UPDATE:
When users login from outside the network, urls are changed. This is what we need to code for. I think I the following is applied twice, from server side and client side (this code is not editable):
s=s.replace(/location.assign\(([^;]*)\)/g,"location.assign(alter_url($1))")
s=s.replace(/location.replace\(([^;]*)\)/g,"location.replace(alter_url($1))")
if(s.match(/location\s*=\s*([^;]*)(;?)/)!=null&&s.match(/\.open\(.+,.+,.*location\s*=.+\)/)==null)
s=s.replace(/location\s*=\s*([^;]*)(;?)/g,"location=alter_url($1)$2")
s=s.replace(/location\.href\s*=\s*([^;]*)(;?)/g,"location.href=alter_url($1)$2")
s=s.replace(/window\.open\(([^,]*)(,.*)?\)/g,"window.open(alter_url($1)$2)")
s=s.replace(/\.src\s*=\s*([^;]*)(;?)/g,".src=alter_url($1)$2")
s=s.replace(/\.action\s*=\s*([^;]*)(;?)/g,".action=alter_url($1)$2")
s=s.replace(/\.innerHTML\s*=\s*([^;]*)(;?)/g,".innerHTML=alter_html($1)$2")
s=s.replace(/\.outerHTML\s*=\s*([^;]*)(;?)/g,".outerHTML=alter_html($1)$2")
Actually, the more I look at this, the more unrealistic it's becoming..
Right-clicking and choosing "Open in new window" doesn't work? Because if not, I'm not seeing a Javascript workaround working either. But:
You can open a new window with a specific URL (e.g., link) in Javascript easily enough:
window.open("http://stackoverflow.com");
And there are ways of getting the text that's selected in a page, to feed into that. All of which can be wrapped up into a bookmarklet so that the action becomes "select the text, click a link on the bookmark toolbar".
But if "Open in new window" doesn't work, I wouldn't expect that to work either.
You can use the window.open to open the links on a new window. In fact, you could use a bookmarklet to set the target to _blank on every link on the site, in case you can't access the application source code.
BTW, if you hold the Shift key when opening the link it will open on a new window.
How to open 2 different link one in same window and another one in new window from one link? I want to open one link in parent window and other one in new window not new tab in all A-Grade browsers
Use a javascript function that first calls window.open and then window.location.
Typically, if you use window.open and specify a height and width for the window it will cause most browsers with most configurations to open it as a new window and not a new tab.
The following will add a popup window to the link with the id link-of-doom. Specify the link that you want the current page to redirect to in the href attribute as you normally do.
HTML
Click me!
JavaScript
$(function() {
$("#link-of-doom").click(function() {
window.open('/page2.html', 'sometarget', 'width=400,height=200');
});
});
* You should not use the onclick attribute in the HTML itself as it is not considered a best practice . . . and a kitten is killed every time someone uses it.
Your link
This may or may not work, depending on whether the 'onclick' handler runs before the standard behaviour of the link.
If it doesn't - or is intermittent - let me know, and I'll supply an alternative approach.
EDIT:
As an alternative, I'm thinking that you could have 2 links, one for the 'new' window and one for the 'current' window. Make the 'current' window link invisible, using css, amd add an 'onclick' handler to the 'new' link, that fires the 'current' link.
Your link
Hidden link
Be sure to check this on multiple browsers.
P.S. I'm assuming that you're using jquery - if not, the code that triggers the 'click' event will need to change.