How to stop a custom bookmarklet from opening multiple browser tabs - javascript

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>

Related

window.open remove reference from parent

I have one page with list of reports and after clicking on report it redirects me to internal report page in new tab with:
window.open(reportIdUrl,reportId);
So when i am back on report list and i want to open same report i will use
window.open("",reportId);
if(redirect.location.href === "about:blank" || redirect.location.href !== '<internalreportpage>') {
redirect = window.open(reportUrl,reportId);
redirect.focus();
} else {
redirect.focus();
}
But when someone from new tab with internal report page navigates somehow to report list page (within this tab) and then tries to open internal report page it will open it in same tab as it is tab reference not content reference.
Does anybody know some way to drop reference when i access if condition?
Something like:
window.open("",reportId);
if(redirect.location.href === "about:blank" || redirect.location.href !== '<internalreportpage>') {
window.dropReference(reportId) //change_me
redirect = window.open(reportUrl,reportId);
redirect.focus();
} else {
redirect.focus();
}
So it will create new tab with new reportId reference?
Thanks
EXAMPLE
--reportlistpage
linkToReport1 (window.open(report1Url,1))
linkToReport2 (window.open(report2Url,2))
.
.
.
You click on linkToReport1 - 2 tabs are opened. One with reportlistpage and one with internal-report/report1.
You go back to parent tab and click to linkToReport1. Tab is opened with reference to "1" and will focus to it and no new tab is opened (this is ok).
You are on linkToReport1 and you redirect with some menu hyperlink to reportlistpage (within linkToReport1 tab). Url changed to /report-list
You click to linkToReport1. Nothing happen (this is not ok) because you are on the tab with reference to 1 (content and url changed), now you want to open a new tab with /internal-report/report1 url and store it as 1 with window references.
I figured it out. It is bit simple when you realize you can set or reset window.name. So there is nothing like references from parent.
Based on content you can do something following. In the page you want to keep track in tab,
at the beginning (in my case internalReport id):
var currentWindow = window.self;
currentWindow.name = reportId; //this is in case someone manually opened it without window.open(internalReportUrl, reportId)
then bind resetting of window.name on beforeunload event (e.g.):
var resetWindowName = function() {
var currentWindow = window.self;
currentWindow.name = ""
}
window.addEventListener('beforeunload', resetWindowName);
So when you redirect from internalreportpage somewhere, name is cleared and you can repeadetly call
window.open(internalReportUrl,reportId)
which will open new tab for you.

Javascript/Html.. push button goes into currect tab not new one

I'm trying to make a random link picker - When I press the button, the link goes in the current tab. I'm trying to make it so it will go into a new one.
<script type="text/javascript">
function randomlinks(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="link1.com"
links[1]="link2.com"
links[2]="link3.com"
window.location=links[myrandom]
}
</script>
<form>
<input type="button" value="Random Episode!" onClick="randomlinks()">
</form>
Change it from:
//This sets the location of the current window, which you don't want
window.location = links[myrandom];
To this:
//This says to open a new tab or window, based on the user browser settings, to the url
window.open(links[myrandom]);
Just replace the line
window.location=links[myrandom]
with
window.open(links[myRandom], '_blank');
The target attribute specifies where to open the linked document.
You also have to check your browser settings first. If they are set to new window you can click your whole lifetime and it will still be opened in a new window instead of a new tab.
These are the right attributes.
The target attribute can have one of the following values:
_blank - Opens the linked document in a new window or tab
_self - Opens the linked document in the same window/tab as it was clicked (this is default)
_parent - Opens the linked document in the parent frame
_top - Opens the linked document in the full body of the window
framename - Opens the linked document in a named frame
So, this means that the default value (without the target attribute)
window.location=links[myrandom]
is also working. Hope it helps that your scripts work lovely.

How to open new HTML tab after clicking on the button

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

How to set the title for the new browser tab?

I have a question about the new tab for the link.
Is there anyway I can set the browser tab title before user clicks a link? It seems like there is no way to debate the title for the new tab if the html contained in the new tab doesn't have title attribute. Am I right? How do I set the title?
//the href is dynamic so I can't set them one by one because I have 100+ html file here
<a href="test.html" target="_blank">open me<a>
As you have it, this is not possible because your links are just normal HTML links. When the new page opens in a new tab, the current page will not have any reference to it and so cannot change it in any way. You will need to open the page using javascript and set the title that way.
You can dynamically set this up in window onload to find all a tags and add a click event whihc opens the window and sets the title.
If you want different titles for each page, you can store this in a data- attribute in the a tag.
Note tho that this will only work with pages in the same domain (for security), and that it does not handle people right clicking and pressing "Open in New Window". Middle click in Windows does seem to work however.
HTML
open me
JavaScript
window.addEventListener("load", function() {
// does the actual opening
function openWindow(event) {
event = event || window.event;
// find the url and title to set
var href = this.getAttribute("href");
var newTitle = this.getAttribute("data-title");
// or if you work the title out some other way...
// var newTitle = "Some constant string";
// open the window
var newWin = window.open(href, "_blank");
// add a load listener to the window so that the title gets changed on page load
newWin.addEventListener("load", function() {
newWin.document.title = newTitle;
});
// stop the default `a` link or you will get 2 new windows!
event.returnValue = false;
}
// find all a tags opening in a new window
var links = document.querySelectorAll("a[target=_blank][data-title]");
// or this if you don't want to store custom titles with each link
//var links = document.querySelectorAll("a[target=_blank]");
// add a click event for each so we can do our own thing
for(var i = 0; i < links.length; i++) {
links[i].addEventListener("click", openWindow.bind(links[i]));
}
});
Sample JsFiddle
You can pass the title with hash and get it on another page, if this another page is yours and you can modify its code.
1st page:
...
<a href="test.html#the_title_you_want" target="_blank">open me<a>
...
2nd page - modify the body opening tag like this:
<body onload="document.title=window.location.hash.replace('#','');">
If the page you are linking to isn't yours, you can use window.open method:
open me
I have not seen addEventListener work reliably, especially when opening a new page using javascript. The best way to change the tab title and have it work reliably is to set a timeout until the page loads. You may have to play with the timeout value, but it works.
var newWindow = window.open(url, '_blank');
setTimeout(function () {
newWindow.document.title = "My Tab Name";
}, 100);
You have two options. Using pure HTML, you can let the user open up links, then later on change the title. Or you can change the title with inline JavaScript. Here's how you do both:
Method 1
Change your links by assigning a target attribute, and then later on use that window name to control the document. For instance in your links it would be: <a href="whatever" target="theNewWindow">. Whenever you want to change the title for this page, you'd use JavaScript as such: window.open("", "theNewWindow").document.title = "New Page Title!"; The problem with this method however is that all links with that target/window name will open in that same window. In addition, after the first time the link is clicked, your browser won't automatically switch to the new tab/window.
Method 2
Change your links by assigning an onclick attribute, which would open the link manually and change the title of the page immediately. Basically it would come down to look like: <a href="whatever" onclick="var w=window.open(this.href, '_blank'); (w.onload=function(){w.document.title='New Page Title!';})(); return false;">. This opens the window based on the href attribute, immediately changes the title, and sets the window to change the title to that when it finishes loading (just in case there really was a title tag).
The problem with both of these methods (as mentioned by others) is your html files have to be on the same domain.
The simplest way is a follows:
var winTab = window.open("", "_blank")
//Open URL by writing iframe with given URL
winTab.document.write("write iframe with your url in src here")
//Set Title for the new tab
winTab.document.title = "Form Title"
You could make your own Page 2 that opens up the other pages (the ones you can't edit), in a frameset. You can then either change the title dynamically when loading your page 2, or as others have suggested if you use window.open you can control the title from the parent page.
If you are in page 1, and opening page 2 in a new tab, you can't set title for page 2 from page 1.
If you have access to page 2 then it's possible, otherwise not.

Open link in new tab in firefox-extension

I have developed a webapp to use it as Firefox extension. In Firefox I include it with an iframe like this
<iframe src="http://mywebapp.com" flex="2" id="browserTable" name="table_frame"/>
Now I want to have some outgoing links in my app. If I just use normal link markup like
Contact
the link is opened in the iframe that is small in space since it is in the sidebar. Is there any way to open it in a new tab in the main browser window?
The target attribute allows you to specify which window to open a link in. You have these special keywords you can place in the attribute:
_blank - new window
_self - same window (default)
_parent - the window which opened the current window, or the parent frame in a frameset
_top - overload the entire page, usually used in a frame context
"string" - in the window with an id of "string", or a new window if "string" is not the id of a current window
So, here is your HTML:
Contact
EDIT Did some research after our discussion in comments, and found this snippet:
var myUrl = "http://mesh.typepad.com";
var tBrowser = top.document.getElementById("content");
var tab = tBrowser.addTab(myUrl);
// use this line to focus the new tab, otherwise it will open in background
tBrowser.selectedTab = tab;
Source: http://mesh.typepad.com/blog/2004/11/creating_a_new_.html
Let me know if that works out... curious myself, but my current FF environment is not one in which I can easily experiment with extension dev, and I don't want to change things to try.

Categories