open email client through javascript - javascript

is there anyway through which I can open more than one email client, (on a single click) in java script, I know how to use mailto but don't know how to open multiple clients
this code opens the client on each reload.
window.location.href = "mailto:user#example.com?subject=Subject&body=message%20goes%20here";
Any help in this regard Thanks

If you want it to load the mail client on a click rather than every time the page refreshes, you want it attached to a click event, something like this :
<button class="button">Open Email</button>
Using jQuery :
$(document).ready(function(){
$('.button').on('click',function(){
window.location.href = "mailto:user#example.com?subject=Subject&body=message%20goes%20here";
});
});
Update
If you want it to load multiple instances of the client, just duplicate the window.location.href :
$(document).ready(function(){
$('.button').on('click',function(){
window.location.href = "mailto:user#example.com?subject=Subject&body=message%20goes%20here";
window.location.href = "mailto:user#example.com?subject=Subject2&body=message%20goes%20here";
});
});

It is not possible to launch external applications from JavaScript in a Browser.
mailto only launches the MUA which is configured as the default in the system-settings.

Related

How Can I go to another HTML file

For some reason, document.location.href , window.location.href , and window.location.replace is not working, I'm using localhost for now and I don't know how to redirect it to another webpage. I tried adding http:// on front, it's still not working, I tried making it redirect to another online website, It also didn't work. I read a lot of thread already and tried them but none of them worked for me.
var loginBut = document.getElementById("RDbtn1");
loginBut.addEventListener("click", checklogin);
function check(){
document.location.href = "http://localhost/some/directory/here";
alert(document.location.origin + '/some/directory'); // I just use this to know that the button is being pressed.
}
html:
<!-- some code here-->
<li><button class="login1" id="RDbtn1">LOGIN</button></li>
<!-- some code here -->
I believe you should redirect to particular html page and should give the port on which your local host application is working
document.location.href = "http://localhost:8080/some/directory/here/file.html";
It is window.location (nothing or replace or href) - document.location is normally used to get the actual page
What's the difference between window.location and document.location in JavaScript?
If you are on the same server, no need to qualify the page but I would add the actual page you try to load
window.location = "/some/directory/here/index.html"

How to close html file javascript

I'm opening a new html file on button press using this:
window.location = "menu.html";
I want to go back to the index.html file in my "menu" activity after a button press.
I tried using
window.location = "index.html";
But it creates a new screen and if I click the return buton to go to my homepage it returns to the previous activities. Also
window.opener.location = '/redirect.html';
window.close();
Doesn't work. So how do I close the menu activity to go back to my main activity.
PS: should I use window.location or window.location.href to open new html file
Thanks in advance!
Try using window.open("menu.html", "menu"). In addition to opening the file you can pass in a second parameter to the .open method and the next time you want to open the menu.html file you can using window.open("menu.html", "menu") it will open the already opened file in the browser.
https://developer.mozilla.org/en-US/docs/Web/API/Window/open
I fixed it using
history.go(-1);
navigator.app.backHistory();
sugested in this answer:
Phonegap - navigator.app.backHistory() not working on HTML back button
Also the href on the end of window.location.href is used for links so you should use just window.location

JS/jQuery mailto AND loading other site

My situation is as follow:
I have the sites:
preview.html and index.html
Now I have some inputs and a button on preview.html, which should open the email programm using mailto and load index.html where preview.html was displayed
I have tried
window.location.href = "mailto:[myadress]?subject=[formname]&body[his inputs]";
window.location = "index.html"
Does anyone know how I could solve it?
The closest you can get client side - if the browser allows you is
window.open("mailto:[myadress]?subject=[formname]&body[his inputs]","_blank");
window.location = "index.html"
or using an iFrame:
window.iFramename.location="mailto:[myadress]?subject=[formname]&body[his inputs]";
setTimeout(function() {
window.location = "index.html";
}),1000);
but a better solution is to mail at the server and redirect

Javascript: Clicking Link to Download pdf

I am working on a JS program which should open a webpage www.mysite.com & click on a link inside that webpage to download a pdf.
The link to click looks like this:
<a onclick="download();return false;" href="#noWhere">Click to Download</a>
Ordinarily, manually clicking the link, calls the following function to download the pdf:
function download() {
document.forms[0].action = path + "/xxW04_sv_0140Action.do";
document.forms[0].target = "_self";
document.forms[0].submit();
}
My code is simplified javascript code to open the page & click on the "Click to Download" button is this:
<script>
var linkname = "http://www.mysite.com";
var windowname = "window_1"
// Opens a new window
var myWindow = window.open(linkname, windowname ,"width=400,height=600");
//should open a link to download pdf
myWindow.document.getElementById('href = \"#noWhere\"').click();
</script>
So far I can open the webpage "mysite.com" in a seperate window using but for some reason no button clicking is happening and certainly no pdf is downloaded.
Of course if I manually click the "Click to Download" button it downloads.
Can anyone tell me what i'm doing wrong? Why I cannot simulate a click with the above js code?
Or possibly give me some things to try. Any help much appreciated and Than you.
UPDATE:
From the initial answers below, possibly this method is doomed for failure! Can anyone suggest a better way I could be downloading these pdfs?
You'd better use:
<a href="http://www.mysite.com/mypdf.pdf">
This should download that pdf file.
It won't work. The same-origin policy will prevent you from accessing the content of any pages loaded from another domain.
Also, as #kamilkp pointed out, you have to provide the getElementById() function with an id value. You can't just plug any old stuff in there and expect it to work.
Another problem is your reliance on clicks for this to work. What about users that use the tab key to select links and then press Enter to follow the link?

Auto-download behavior and back button issue

Instead of linking directly to files for download on a web site we link to a page that says "thank you for downloading". The page has tracking codes on it so we know how many people have downloaded the file. The page launches the download file using the jQuery code shown which adds a short delay after the page loads before the download begins. The download location has a content disposition header so it always downloads properly in the browser leaving the "thank you for downloading" page visible. This all works well.
The problem comes if the user carries on browsing past this page and then hits back. The download fires again.
Using window.location.replace(href); didn't seem to fix it.
The issue is further complicated by the fact that the CMS delivering the page has set it to expire immediately so it's not being cached.
Suggestions for (i) ways to avoid this problem; (ii) any better ways to handle file download / thank you pages?
jQuery Code
$(document).ready(function () {
$('a.autoDownload').each(function () {
setTimeout('navigateToDownload("' + $(this).attr('href') + '")', 4000);
});
});
function navigateToDownload(href) {
document.location.href = href;
}
One possible approach would be to set a cookie via Javascript when the page first loads. Then, if that page is ever loaded again, you can check for the presence of the cookie, and if present, do not execute the auto download?
Using the Cookie plugin for jQuery as an example:
$(document).ready(function () {
$('a.autoDownload').each(function () {
var hasDownloadedThisLink = $.cookie("site." + $(this).attr('id'));
if (!hasDownloadedThisLink) {
$.cookie("site." + $(this).attr('id'), "true");
setTimeout('navigateToDownload("' + $(this).attr('href') + '")', 4000);
}
});
});
This is just an example. If you went this way, you'd have to consider how many possible download links there might be, as there is a limit on how many cookies you can set. Also notice that I used an id attribute of the links to identify them in the cookie - I figured this would be more suitable that using some form the href attribute. I also prefixed the cookie name with site..
Well there are a couple of solutions to this. Here's an example:
function navigateToDownload(href){
var e = document.createElement("iframe");
e.src=href;
e.style.display='none';
document.body.appendChild(e);
}
Other implemenatations might exist, but I doubt they're less "hacky".

Categories