2011-06-21 25 views
1

我想要一個簡單的書籤工作與Rails創建一個窗體與當前頁面標題和URL並提交創建一個對象。返回undefined在bookmarklet - 轉發頁

我遇到的問題是,當表單提交時,它轉發到「成功」頁面,而不是僅僅停留在同一頁面上。

我知道小書籤代碼應該返回undefined以阻止這種情況發生,但我試過把void(0)放在任何我能想到的地方,但它仍然轉發到下一頁。

這裏有兩種方法我試過:

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); 
})()); 

和:

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); 
})(); 

能否其他.js文件是造成問題的原因?那就是:

(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(); 
})(); 

我試圖返回undefined有作爲,但轉發行爲並沒有改變。它可能與軌道處理表單發佈請求的方式有關嗎?

+0

當您提交表單時,爲什麼它應該保持在同一頁上?默認情況下,當您發送表單時,您會轉到表單操作,並且代碼中沒有任何內容可以阻止此行爲。 –

+0

啊,謝謝。我正在嘗試通過表單提交頁面信息,類似於Instapaper和其他書籤書籤。從我可以告訴我需要做的事情,如打開一個新的iframe來做到這一點。 – bobfet1

回答

1

首先,void(0)用於獲取未定義的返回值,這是停止轉到下一頁的一種方法,但不適用於此處。並且,確保在小書籤的末尾保持無效(0)。

我能想到停止去到下一個頁面是先創建一個不可見的內嵌框架的一種方式(IFRAME)通過設置newform

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) 

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

然後與目標,這是在這裏biggy部分:

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

我認爲就是這樣。

另外:

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) 

這就是無效應該去^^

1

添加 「的onsubmit」 處理器的 「newform」,並使用一個XMLHttpRequest發送數據。

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