2010-11-08 72 views
0

我在使用http://snippets.dzone.com/posts/show/4973scrollTop建議的一些JavaScript來創建一個書籤,用於在Blogger的新帖子textarea中插入一個預設的字符串。代碼如下所示:scrollTop輸出而不是設置?

//IE support 
if (document.selection) { 
    myField.focus(); 

    //in effect we are creating a text range with zero 
    //length at the cursor location and replacing it 
    //with myValue 
    sel = document.selection.createRange(); 
    sel.text = myValue; 

//Mozilla/Firefox/Netscape 7+ support 
} else if (myField.selectionStart || myField.selectionStart == '0') { 

    myField.focus(); 
    //Here we get the start and end points of the 
    //selection. Then we create substrings up to the 
    //start of the selection and from the end point 
    //of the selection to the end of the field value. 
    //Then we concatenate the first substring, myValue, 
    //and the second substring to get the new value. 
    var startPos = myField.selectionStart; 
    var endPos = myField.selectionEnd; 
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); 
    myField.setSelectionRange(endPos+myValue.length, endPos+myValue.length); 
} else { 
    myField.value += myValue; 
} 

}

其下方的建議:

//add this to the start of function 
textAreaScrollPosition = myField.scrollTop; 

//add this to end of the function 
myField.scrollTop = textAreaScrollPosition; 

scrollTop建議在Firefox中失敗,而不是用的值替換在瀏覽器中當前頁面textAreaScrollPosition

我已將此添加的夾下來版本的書籤正面:

javascript:var myField=document.getElementById('postingHtmlBox');var myValue='lol'; 

共記載:

javascript:var myField=document.getElementById('postingHtmlBox'); 
var myValue='lol'; 
var textAreaScrollPosition=myField.scrollTop; 
if(document.selection){myField.focus(); 
sel=document.selection.createRange(); 
sel.text=myValue; 
}else if(myField.selectionStart||myField.selectionStart=='0'){myField.focus(); 
var startPos=myField.selectionStart; 
var endPos=myField.selectionEnd; 
myField.value=myField.value.substring(0,startPos)+myValue+myField.value.substring(endPos,myField.value.length); 
myField.setSelectionRange(endPos+myValue.length,endPos+myValue.length); 
}else{myField.value+=myValue; 
}myField.scrollTop=textAreaScrollPosition; 

不換行,雖然。

我比JS嚮導少。我只是想幫助一個非技術性的朋友做一些與Blogger有點複雜的事情。有任何想法嗎?

編輯:除了添加原始頁面檢測,並提示框替換預設的文字,我能夠加入​​到底解決了原來的問題:

javascript:if(document.getElementById('postingHtmlBox')){var myField=document.getElementById('postingHtmlBox'); 
var myValue=prompt('Insert text here.'); 
var textAreaScrollPosition=myField.scrollTop; 
if(document.selection){myField.focus(); 
sel=document.selection.createRange(); 
sel.text=myValue; 
}else if(myField.selectionStart||myField.selectionStart=='0'){myField.focus(); 
var startPos=myField.selectionStart; 
var endPos=myField.selectionEnd; 
myField.value=myField.value.substring(0,startPos)+myValue+myField.value.substring(endPos,myField.value.length); 
myField.setSelectionRange(endPos+myValue.length,endPos+myValue.length); 
}else{myField.value+=myValue; 
}myField.scrollTop=textAreaScrollPosition; 
myField.focus(); 
}; 

如果沒有把握最後一個分號是絕對必要或不是,但哦,解決方案!

回答

0

根據編輯的問題,加入​​到最後解決了這個問題。

0

您可能會使書籤太長而無法放入書籤。一個選項是使用動態腳本標記:

javascript:document.body.appendChild(document.createElement('script')).setAttribute('src','http://mysite/myscript.js') 

其中myscript.js是執行該工作的實際腳本。

如果你把它作爲一個自包含的小書籤,一定要用花括號包圍整個事物(在「javascript:」之後)。

+0

一個提示:爲了開發目的,將腳本url從「http://...whatever.js」更改爲「http://...whatever.js?」+ Math.random()... 。這將確保您始終獲得最新版本的文件。你會發現這使得書籤開發變得更容易,因爲你並不總是必須編輯書籤本身,只需編輯Web服務器上的文件即可。如果完成後您可以將其壓縮爲一個獨立的書籤,如果不是,請使用託管版本。 – rob 2010-11-08 23:04:17

+0

謝謝,搶。直到現在我還沒有意識到這種方法。我嘗試通過減小書籤的長度來測試長度:... javascript:var myField = document.getElementById('postingHtmlBox'); var textAreaScrollPosition = myField.scrollTop; myField.scrollTop = textAreaScrollPosition; ...不幸的是,同樣的錯誤發生。 – CartoonChess 2010-11-08 23:26:00

+0

上面編輯了我的回覆。缺乏大括號似乎是問題...我認爲這是必要的,如果你在一個小書籤中聲明變量。 – rob 2010-11-08 23:42:14