Window that opens and closes [duplicate] - javascript

I am using this javascript function to launch download
function startDownload(url) {
window.open(url, 'Download');
}
It works, but i want to block any new tab or new window launch, thanks.

function startDownload(url) {
window.location.href = url;
}
This will start the download in the same page, exactly like when you click a link without any target other than _self.
To force the download of a file, make sure you send the right headers with it:
Content-Disposition: attachment; filename="mypdf.pdf";
This will make sure that the file is not displayed in the browser instead of being downloaded. Replace the filename part with the filename you want as default on the save as dialog.

window.open will open a new window \ tab (depending on user preferences) ... to just download the file use
window.location.href = url;
You can use this if the url returns a downloadable file rather than a web page

HTML5 solution with 'download' attribute
<a href="/images/myw3schoolsimage.jpg" download>
https://www.w3schools.com/tags/att_a_download.asp

<a target="_parent" href="link"></a>
_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

Related

How to use Javascript to open a link (external website) without HTTPS protocol?

This code (with HTTPS protocol), successfully opens https://www.google.com.
JSFiddle
<script>
function openInNewTab() {
var url = 'https://www.google.com';
var win = window.open(url, '_blank');
win.focus();
}
</script>
<div onclick="openInNewTab();">OPEN LINK ON A NEW WINDOW</div>
But when I use this code (without HTTP protocol), it opens https://www.example.com/www.google.com instead.
JSFiddle
<script>
function openInNewTab() {
var url = 'www.google.com';
var win = window.open(url, '_blank');
win.focus();
}
</script>
<div onclick="openInNewTab();">OPEN LINK ON A NEW WINDOW</div>
Is there a way to use Javascript to open an external website without HTTPS protocol?
Why not just use an external anchor tag with the target set to _blank?
E.g
OPEN LINK A NEW WINDOWS
Although in this example, google will redirect to https because security
A URL string without a protocol at the beginning (or the "protocol-relative shorthand" // for which there are good arguments to not use it) will be considered either an anchor URL if it begins with a hash, #fragment, or otherwise a relative URL. A relative URL just puts www.google.com on top of the current level of the path. If your location is http://www.example.com then that becomes http://www.example.com/www.google.com. Or if it's http://foo.net/bar/baz/quux.html, that becomes http://foo.net/bar/baz/www.google.com.
var url = new URL('http://example.com');
Keep in mind that when you are opening http://google.com they have a redirect to their https protocol. Not much you can do about that.

Open Base64 in new tab

I have a Base64 encoded document which can be PDF file or image.
I would like to create a button in HTML5 page that opens this base64 in a new tab (or new page it does not matter)
I found this code can do the trick :
<a href="http://chriscoyier.net"
onclick="window.open(this.href); return false;"
onkeypress="window.open(this.href); return false;">
This link will open in new window/tab
</a>
It works well.
But when I replace the http link with
href="data:application/octet-stream;base64,/9j/4A.."
then the file is downloaded but not displayed in browser.
Your MIME type is set to 'application/octet-stream', which will generally be downloaded and not displayed (depending on browser and system settings). If you are trying to load browser-displayable content, then you should use a more appropriate MIME type for your content so that it displays inline and does not get downloaded.
Please note: As of Chrome 60 and an upcoming version of FireFox (although it appears FireFox will still support images), data URIs cannot be opened in the top-level frame of the browser (and it was never supported in IE/Edge), but they can be opened in iframes and img elements.
The workaround below is tested and working in the latest version of the above browsers. This could also be rewritten with an img tag to display an image.
<button id='btnDownload'>Download</button>
document.getElementById('btnDownload').addEventListener('click', function(){
var w = window.open('about:blank');
setTimeout(function(){ //FireFox seems to require a setTimeout for this to work.
w.document.body.appendChild(w.document.createElement('iframe'))
.src = 'data:application/octet-stream;base64,SWYgSSBoYWQgYSBuaWNrbGUgZm9yIGV2ZXJ5IHRpbWUgSSBoYWQgYSBuaWNrbGUsIEknZCBoYXZlIGVhdGVuIHR3aWNlIGFzIG1hbnkgcGlja2xlcy4=';
}, 0);
});
If, however, your intention is to just download the content (contrary to the stated intent in the question, but applicable to 'application/octet-stream' content in general), this should suffice:
<a href='data:application/octet-stream;base64,SWYgSSBoYWQgYSBuaWNrbGUgZm9yIGV2ZXJ5IHRpbWUgSSBoYWQgYSBuaWNrbGUsIEknZCBoYXZlIGVhdGVuIHR3aWNlIGFzIG1hbnkgcGlja2xlcy4=' download>Download this</a>
Tested on chrome and Firefox, just add the base64 value into the href attribute works (without onclick & onkeypress events). Just like that :
<html>
<body>
test
</body>
</html>
I am adding the answer to my question based on Aaron's answer:
document.getElementById('btnDownload').addEventListener('click', function(){
var w = window.open('about:blank');
setTimeout(function(){ //FireFox seems to require a setTimeout for this to
work.
w.document.body.appendChild(w.document.createElement('iframe'))
.src = $this.attr('href');
w.document.getElementsByTagName("iframe")[0].style.width = '100%';
w.document.getElementsByTagName("iframe")[0].style.height = '100%';
}, 0);
});
<a href="aHR0cHM6Ly93d3cuc3dpdGNoLXRvcnJlbnRzLmNvbS9kb3dubG9hZC80MTMvU3VwZXIlMjBTbWFzaCUyMEJyb3MlMjBVbHRpbWF0ZS50b3JyZW50"
onclick="window.open(this.href); return false;"
onkeypress="window.open(this.href); return false;">
This link will open in new window/tab
</a>

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>

How can I redirect just a single blogger.com page to an external URL with Target Blank

I am using the following code in my blog, but this script opens the external URL in the native tab itself. I want to redirect it to a new tab altogether.
<script type="text/javascript">
window.location = "http://externalblog.blogger.com/externalpost";
</script>
Try window.open("http://externalblog.blogger.com/externalpost"); This should work.
Use the window.open method to open in a new window / tab
window.open(strUrl, strWindowName[, strWindowFeatures]);
Refer to MDN for details.
Here is an example
var windowObjectReference;
var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
function openRequestedPopup() {
windowObjectReference = window.open("http://externalblog.blogger.com/externalpost", "Blogger Window", strWindowFeatures);
}
Call the openRequestedPopup() method on whatever condition you want it to open. Alternately, if you already have the link while rendering the HTML, use the target="_blank" attribute in the anchor tag.

How to redirect after closing modal frame window

I close my modal frame window in javascript with such command
Drupal.modalFrameChild.triggerParentEvent('childClose', [args, statusMessages]).
But I don't know how to redirect my browser to a specific path after that?
You can pass the window.location.href = url; command. Just use the url to define the location!

Categories