4

I'd like to create a bookmark for use in the toolbar of Firefox that opens a JavaScript prompt window once clicked, and asks for user input.

Firstly, is this possible to do?
Secondly, how can I submit a JS input field to a specific PHP page for processing?

Thanks

Dean
  • 755
  • 3
  • 15
  • 31

2 Answers2

7

Here is one of my bookmarks:

 var answer = prompt('Sitename?');  
 window.location.href = "http://www.ukraine.com.ua/Domains/Whois/?domain=" + answer;

I use it for viewing info for a domain.

c0deNinja
  • 3,956
  • 1
  • 29
  • 45
primetwig
  • 465
  • 3
  • 12
1

Yes this is possible, try something like this:

# bookmarklet
<a href="javascript:var answer = prompt('What is your name?'); var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'; head.appendChild(script); $.post('http://localhost/script.php', { name: answer });void 0;">prompt_and_post</a>

a more readable version:

var answer = prompt('What is your name?');
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
     # load jquery
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js';
head.appendChild(script);
     # do a jquery post
$.post('http://localhost/script.php', { name: answer }); void 0;
erickb
  • 6,193
  • 4
  • 24
  • 19
  • It's for a POST, not get. I tried XHR and it wont work on any page though. And ultimately I'd like to stay on the page I'm on. Its for microblogging use. http://pastebin.com/ts8Hac3Z – Dean Feb 08 '11 at 13:02
  • @Dean, I have edited my answer. I simply use jquery for posting. It will be loaded into the of the current page. – erickb Feb 08 '11 at 13:27
  • @erickb thanks, but that doesnt seem to work still.. I have noticed in Firebug it does load into the section (jQ). However the page does nothing, and if pressed for a second time it returns '[object Object]'. – Dean Feb 08 '11 at 13:37
  • @Dean I added void 0, so it won't proceed to the actual link of the bookmarklet '[object Object]'. – erickb Feb 08 '11 at 13:51
  • @erickb thanks for the help, but that hasnt worked either. It's stopped the [object Object] problem but it still wont actually post data. Another person is saying I should use an onLoad handler with this, because the jQuery only begins loading. – Dean Feb 08 '11 at 13:55
  • @Dean I was puzzled by this, all the while I had been testing it on localhost(same domain). This seems to be a same origin policy type of error, [look here](http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox) and [here](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy). – erickb Feb 08 '11 at 19:07