Search code examples
javascriptformsbookmarklet

Submit button with enter an url


I'm trying to create a bookmarklet, which captures the url of the current page, where the user is, puts this url into a text field of a form on a page and then submits the form by virtually pressing submit button.

With following code i get the url of the current page, go to the site with url http://example.com/?url=http://www.url-of-the-current-page, fill the url http://www.url-of-the-current-page into the form's text field, but the form itself remains unsubmitted:

javascript:(function(){ window.open('http://example.com/?url='+encodeURIComponent(location.href))})();

But how can i submit the form button? The whole form looks like:

<form ng-submit="launchTest()" class="ng-pristine ng-valid"> 
<input type="text" name="url" ng-model="url"> 
<input type="submit" value="Launch test" class="launchBtn" ng-class="{disabled: !url}"> 
</form> 

I've tried two variants - and failed: in both variants i stay on http://example.com?url=http://www.url-of-the-current-page:

javascript:(function(){ window.open('http://example.com/?url='+encodeURIComponent(location.href));document.forms[0].submit()})();

javascript:(function(){ window.open('http://example.com/?url='+encodeURIComponent(location.href));document.forms[this.form.id].submit()})();

Solution

  • (Posted on behalf of the OP).

    I've contacted the owner of the website, where I wanted to run my bookmarklet - he said, my bookmarklet is correct, but for submitting the form the url should contain a special parameter, like &run=1. With this parameter every bookmarklet mentioned in this thread works (works means not only opens new tab and inputs the url, but submits the form too). The working bookmarklet I'll use is:

    javascript:(function(){var win=window.open('http://example.com?url='+encodeURIComponent(window.location.href )+'&run=1','_blank');win.focus();})()