-4

I need to open a URL, then type there, and I was wondering if you could do that in javaScript, how, and if not, are there any other languages I can do that in. Remember: No HTML Or CSS and I use Opera. Thanks, - Beginning Coder

Elaborating, I am trying to open my email or Gmail ( I can do that part) and then type in who it is to, etc. Please Help. + Thanks for the feedback and tips though

Cl Garrett
  • 11
  • 6
  • You should read about browser security. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – SudoKid Aug 01 '18 at 23:25
  • Welcome to SO! I went ahead and answered your question b/c you're new, but please take some time to read our site standards (looks like you've attracted some downvotes). This is something you could easily have searched on your favorite search engine. – dandeto Aug 01 '18 at 23:28
  • 1
    You can't directly write from one website to another in a new tab with Javascript. Perhaps you should explain what it is that you're trying to create? There are plenty of other solutions that _might_ do the job. Perhaps through some GET varialbles for example. Or maybe a browser addon like Greasemonkey that detects the new tab and injects the JS for you. – icecub Aug 01 '18 at 23:31

2 Answers2

0

The first part of your question (opening a new tab) is a duplicate of this question: StackOverflow

I'm not entirely sure what you mean by "write," but I answered what I think you're asking.

As for writing into a new window/tab with JS, you can do that 2 ways:

  1. Open a url on your website in a new window/tab
  2. Make a new window and use document.write()

Tutorial: W3 Schools

Now that I know what you want to do specifically, you can do this:

window.open("https://mail.google.com/mail/u/0/#inbox?compose=new");

to open gmail in a new window/tab. You cannot write into any elements in that window (the subject line, for example) with JS because the same-origin policy does not allow it (you do not own google.com). Thus, you cannot accomplish what you want to do unless you are writing into a new window on your own website or file directory.

This StackOverflow post tells you how to set up a mailto that can auto set the subject and body. It's not exactly what you want, but maybe you could adapt it.

dandeto
  • 756
  • 6
  • 13
  • Thanks, that really helped, but do you know any other languages I might be able to accomplish this in ? – Cl Garrett Aug 02 '18 at 13:54
  • You can use Ajax to `POST` data to gmail that will send your email to the intended recipient. https://www.xul.fr/en-xml-ajax.html But no, you cannot manipulate someone else's website with a script from another origin in any language. It is a web standard. – dandeto Aug 02 '18 at 14:20
  • The gmail API may be a better option than Ajax, but it'll be difficult! https://developers.google.com/gmail/api/ – dandeto Aug 02 '18 at 14:28
  • Thanks again, but can I do that with a desktop application like e-mail? – Cl Garrett Aug 02 '18 at 14:38
  • I didn't think your question had anything to do with a desktop application. Gmail (and all other email services) runs on the web, a desktop application is a simple client. JavaScript is a language that runs in the web browser, it has no control over your desktop app. – dandeto Aug 02 '18 at 14:47
  • Thank you so much, final question : Is there a language I could send an email to someone ? – Cl Garrett Aug 02 '18 at 14:54
  • I've already answered that in regards to using an existing mail client, such as Gmail. If you would like to set up your own server and email people with a PHP script, you can learn more about doing so here: https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php. good luck! :) – dandeto Aug 02 '18 at 14:57
0

To open a new tab through JavaScript you can use window.open("link").

And to type in you could make a form in HTML and get the value from that form: document.getElementById("form id from html").value and store it in a variable so you can use it as link in windows.open;

  • I'm a sort of computer noob, but what is a form id from html ? – Cl Garrett Aug 02 '18 at 02:31
  • https://www.w3schools.com/tags/att_global_id.asp – dandeto Aug 02 '18 at 13:18
  • When you make a form in HTML (https://www.w3schools.com/html/html_forms.asp) you can give it an Id(https://www.w3schools.com/Html/html_id.asp), and then in JavaScript where I wrote 'document.getElementById("form id from html").value', when I said "form id from html" I meant the Id that you gave to that form. –  Aug 02 '18 at 23:38