0

How do I make a bookmarklet that places something into a field and submits the form?

I think along these lines:

1)var p = document.open(http://site.com/form.htm)

2) var h = p.innerHTML

3) var f = h.getElementById('formfield')

now how do I get the URL of the current page to become the value for 'formfield'?

tim
  • 3,823
  • 5
  • 34
  • 39

3 Answers3

1

If you are looking to put the current page's URL into formfield, this is how it could be accomplished:

f.value = window.location;
Lucas Green
  • 3,951
  • 22
  • 27
  • Ah. Simple. And would I be able to submit the form simply with h.forms[0].submit? – tim Aug 31 '12 at 07:07
1
var p = document.open(http://site.com/form.htm)

This won't work. You may be thinking of window.open. If you use window.open, it will only be useful for your purposes if the bookmarklet is run from the same domain. If run from any other domain, it will open the window, but you won't be able to do anything else with the document in that newly opened window.

var h = p.innerHTML

This does nothing helpful in your case. It just returns a string of text.

var f = h.getElementById('formfield')

This is not correct because it uses "h", which isn't correct. What you probably want is this...

var w = window.open('http://site.com/form.htm');
// need code that will check if window is done loading before you use next line!
w.document.getElementById('formfield').value = window.location;

If you use the bookmarklet on the page with the form, you only need this:

document.getElementById('formfield').value = window.location;

If you want to open the window to another domain, enter a form value, and submit the form - This can not be done with a bookmarklet. A bookmarklet faces the same restrictions as any other javascript in a page. This is for security to prevent any web page on the internet from trying to take control of your browser and do things on other sites as you. Your only reasonable option in this case would be to create/use a browser addon/extension.

DG.
  • 3,417
  • 2
  • 23
  • 28
  • Ugh. That darn samedomain restriction is driving me crazy. Thanks for clarifying and saving me an hour of fruitless scripting. – tim Aug 31 '12 at 07:23
  • "That darn damedomaon restriction" is absolutely critical to keeping the Internet safe-ish to use. Without it, there would be total chaos. – DG. Aug 31 '12 at 07:24
  • What I have seen though is people doing DOM manipulation at least to some extent, no? I should be able to document.getElementById('formfield').value = window.location; are you certain there is no way that can be done? – tim Aug 31 '12 at 07:25
  • No I get that. But I'm sure I've seen scrips that do things like remove banner ads and such. You know , harmless read only stuff. – tim Aug 31 '12 at 07:28
  • It can be done, but only if you run the bookmarklet while you are on the current page. Then it can do everything and anything that JS can do... but only to that page or another page on the same domain. – DG. Aug 31 '12 at 07:28
0

If I understand correctly, you want to submit the current URL and maybe some other data to your server using a bookmarklet.

I would do it this way:

  1. Append your form to the current DOM using JavaScript. The form should be hardcoded in the bookmarklet.
  2. Populate the form, you are on the guest page now, same domain.
  3. Submit the form, maybe using a target="_blank" for the result.

You can't use Ajax instead of a form to submit your data because of crossdomain restrictions.