Force rel="opener" for links manually opened in new tab - javascript

In my website I have some links like:
My Link
I use rel="opener" because I need the sessionStorage data within the new tab and everything works fine, but I noticed that using the "open link in new tab" command in the right-click menu of the same link hasn't the same behavior.
Looks like the link is opened without the rel="opener" attribute, and sessionStorage data isn't available in the new tab.
I guess it's something related to security, but in my case it's just an intranet website and I would like to know if there's a way to have the same behavior for both cases.
Thanks

Related

JavaScript handling and event listeners

This is within the context of an edge extension but it will work with Standard JavaScript.
The extension must;
1 Be able to recognise when a new tab is opened.
2 Replace the new tab with a designated URL rather than about:blank etc.
3 Have links that appear in popups open within the active tab, not a new one.
I have this JavaScript;
function handleCreated(tab) {
var newTabURL = {url: ''};
browser.tabs.update(newTabURL);
}
browser.tabs.onCreated.addListener(handleCreated);
as an example. It does replace the current tab with google but only when a link in the popup is clicked. The link from the popup should be what replaces the active tab OR if the user creates a new tab it should be replaced with mycompany.com/newtab.html, for example.
I've got an unpackaged example with everything so you can see all of the code. To use this you must first enable developer options in MS Edge (Here's how), then you can load the extension.
The extension in its current state has code notes and explains within the UI. You can grab the extension in its current form from this link. You'll just need to download and unzip then load folder within edge. I figured this is easier than me posting all of the code as you can better see how elements interact with each other.
So in summary;
1 The New Tab should open with the custom URL https://mycompany.com/newtab.html.
2 Typing Google should redirect to https://gooogle.co.uk. (Adding word shortcuts with JS in the address bar)
3 Links opened from the popup should replace the active tab that the popup is available over and not open in new tabs.
It works a little bit, I'm just struggling to get the new tab function to work. This isn't intended for the MS store so the policies aren't relevant.
Download the zipped extension folder here.
MS Edge opens links from a popup in a new tab by default, and this tab isn't active.
If for example you had;
<p>Visit Google</p>
the URL would open in a new tab. To open the URL in the tab that is active within the Edge browser you can instead do the following, so it is actually possible.
1 Place the element in a DIV, for example linked content and use <div id="linkedcontent> within your HTML.
2 Place the content that should trigger the new tab within the DIV.
3 Use the following JavaScript to getElementByID and then use .onclick to replace the current browser window.
document.getElementById("linkedcontent").onclick = function() {
var LinkedContent = {url: 'https://google.com'};
browser.tabs.update(LinkedContent);
};
This will then result in the browser window being replaced by the URL specified. In my example it's https://google.com. If you need to do this multiple times just duplicate the code and change the DIV id to something unique for each and reflect this in the JavaScript too.

Programatically open the URL in new tab in chromium browser using javascript

I have a web application where I have four/five icons in the default page. On click of these icons i am redirecting to some URL in new tab of already opened chromium browser instance using javascript. Below is the javascript code snippet that I am using to launch the URL in new tab of already opened chromium browser instance. Here I am manually clicking the icon
window.open(myURL, '_blank');
But the issue is when I am trying to open the same URL using the same code and programatically triggering the same code to open the URL in new tab, it opes up as a Pop Up but not in a tab. Below is how I am trying to pragmatically open the URL. I am calling the click event of the Icon in the default page using some script rather than manually clicking the icon.
$('#myIconId').click();
If somebody could please help me out. TIA
give a try to use
content of the anchor
Although You shouldn't force user to open new pages or new tab without showing them a hint on what is going to happen before they click on the link.
Above code may or may not work on all browser, please test before making live.
UPDATE
Ref : Programmatically open new pages on Tabs

How to open a link in new window with tab enabled

I search a lot but could not figured it out.
Is it possible to open a new window with tab enabled using window.open() method?.
The way chrome context menu "open link in new window" works.
The browser decides on its own how links will open. Sometimes a user can configure how links are to be opened, but the web page cannot do this.
using an anchor tag you can set the target attribute to have some control over where the link opens. Other than that, this behavior is up to the browser and user and cannot be controlled by javascript.
Use this code
window.open("http:.//www.google.com","DemoName",'scrollbars=1,height=650,width=1050');

Tabbed browsing, programmatically navigating to another open tab

I'm in a discussion with my co-worker. We have a web application in a tabbed browsing environment, say with 2 tabs open. Tab2 has a link to tab1, when I click the link, it opens tab1 in a new tab. We want to detect if tab 1 is already open, if so then set focus to tab1 (and not open a new window).
He has gotten this working in a broswer window environment, but says it is not possible in a tabbed environment.
Is it possible to for javascript to determine if tab1 is already open and then just set focus to that tab?
It is only possible if the webbrowser is already preconfigured to open links with a target in a new tab instead of new window and sets focus to it itself when you have given the links the same target name.
E.g.
link1
link2
link3
In other words: you're dependent on the client/webbrowser used. You have no control over it from the server side on.
In Firefox for example, the client should have checked both the Open new windows in new tab instead and When I open a link in a new tab, switch to it immediately options to get your requirement to work.
(as you see, the last one is disabled in mine)
You should instead create a separate document with a DOM-based tab, and iframes pointing to the real pages. Then you could easily set 'focus' to the correct tab when needed.

Is it possible to copy, paste, open new window in javascript?

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.

Categories

Resources