开发者

returning undefined in bookmarklet - forwards page

开发者 https://www.devze.com 2023-03-14 11:20 出处:网络
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.

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开发者_StackOverflow社区 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)

var myiframe = document.createElement('iframe');
myiframe.scrolling = "no";
myiframe.name = "myiframe";
myiframe.id = "myiframe";
myiframe.src = "blank.html"; //so your users wont have to use their bandwith much, I think you can also set it to the about page in many browsers
myiframe.width = "0px"; //so it becomes invisible
myiframe.height = "0px"; //""
document.body.appendChild(myiframe)

or

var ThisBody = document.body.innerHTML
ThisBody += '<iframe frameborder="0" scrolling="no" name="myiframe" id="myiframe" src="blank.html" width="0px" height="0px"></iframe>

Then by setting your newform with the target, which is the biggy part here:

newform.target = "document.getElementById('myiframe')" //our iframe

I think that's it.

Also:

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)

That's where void should go ^^


Add an "onsubmit" handler to the "newform" and use a XMLHttpRequest to send the data.

    newform.onsubmit = function(event) {
        event.preventDefault();
        // Create XMLHttpRequest object and send data.
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消