0

I am very very new to JavaScript and I would like to build a Bookmarklet for a webservice I use. I need to grab text from any page, load the form of the service and post the text into the textfield of the form. So far I got this:

// grab text
javascript:(function(){var t=window.getSelection?window.getSelection().toString():document.selection.createRange().text;t="You selected: "+t;alert(t);})()

//load form
document.onload=function(){window.location=%22http://www.streetmails.com/index.php%3Fc=mailing%26a=content%22})();

//paste text into form
window.onload=function%20D(a,b){c=b.split('|');d=false;for(q=0;q<c.length;q++){if(c[q]==a)d=true;}return%20d;}function%20E(){f0=document.forms[0];f0['subject'].value='news from London';f0['content'].value='<echo$_GET['t']?>';f0[''].value='Add%20a%20picture...';f0['topicId'].value='4';}E

Can you help me? I searched and keept trying and trying but cant get it running.... Thanks in advance!

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
Nurik
  • 3
  • 1

1 Answers1

0

You can not do exactly what you are trying to do using a bookmarklet. When you execute a bookmarklet, you are executing Javascript inside the current web page. Javascript running in the web page of one web domain can not interact with pages from a different web domain. document.onload will almost never work in a bookmarklet because usually a web page is already loaded before you click the bookmarklet.

Lets call the page with text the "text-page". Lets call the page with the form the "form-page". A web form always submits to another page or to itself. It depends on the form's action value. Lets call that the action-page. Forms can be either submitted via GET or via POST.

To accomplish your goal with a bookmarklet, generally what is done is the bookmarklet will create a form on the text-page and then submit that form directly to the action-page.

If the action-page will accept a GET (only query string values), that is easiest. Look here for many such examples: https://www.squarefree.com/bookmarklets/search.html

If the action-page requires a POST, that is a little more complex because you must use Javascript to create the form: http://www.google.com/search?q=javascript%20create%20form

Addons, extensions, and user scripts can be used to do what you want more directly because they are not limited to operating only in the current domain, but of course they are more complicated to program.

DG.
  • 3,417
  • 2
  • 23
  • 28
  • Thank you for answering! I will try out the links and will get back asap. Happy New Years! – Nurik Dec 29 '12 at 21:18