I’m trying to get a simple bookmarklet working with Rails that creates a form with the current page title and URL and submits to create an object.
The problem I’m running into is that when the form submits, it forwards to a “success” page rather than just staying on the same page as it should.
I know the bookmarklet code should return undefined to stop this from happening, but I’ve tried putting void(0) everywhere I can think of but it still forwards on to the next page.
Here are two ways I’ve tried:
javascript:void((function(){
var s = document.createElement('script');
s.setAttribute('language','javascript');
s.setAttribute('src','http://localhost:3000/script/bookmarklet.js');
document.body.appendChild(s);
})());
and:
javascript:(function(){
var s = document.createElement('script');
s.setAttribute('language','javascript');
s.setAttribute('src','http://localhost:3000/script/bookmarklet.js');
document.body.appendChild(s);
void(0);
})();
Could the other .js file be causing the problem? Here it is:
(function() {
function create_form() {
var path = "http://localhost:3000/links";
var newform = document.createElement('form');
newform.setAttribute("method", "post");
newform.setAttribute("action", path);
newform.setAttribute("accept-charset","UTF-8");
var title_hidden_field = document.createElement("input");
title_hidden_field.setAttribute("type","hidden");
title_hidden_field.setAttribute("name", "link[title]");
title_hidden_field.setAttribute("value", "Test Title");
newform.appendChild(title_hidden_field);
var url_hidden_field = document.createElement("input");
url_hidden_field.setAttribute("type","hidden");
url_hidden_field.setAttribute("name", "link[url]");
url_hidden_field.setAttribute("value", "http://www.example.com");
newform.appendChild(url_hidden_field);
document.body.appendChild(newform);
newform.submit();
};
create_form();
})();
I tried returning undefined there as well, but the forwarding behavior didn’t change. Could it have something to do with the way rails handles the form post request?
Firstly, void(0) is used to get the return value as undefined which is one way to stop going to next page, but does not apply here. And also, make sure you keep void(0) at the end of the bookmarklet.
The one way I can think to stop going on to the next page is to first create an invisible inline-frame(iframe)
or
Then by setting your newform with the target, which is the biggy part here:
I think that’s it.
Also:
That’s where void should go ^^