2011-05-13 65 views
1

我嘗試添加在使用JavaScript和的Greasemonkey在Firefox谷歌的主頁底部的鏈接,但我不能讓它工作:添加鏈接使用JavaScript

// ==UserScript== 
// @name testing greasemonkey 
// @include http://*google.com/ 
// ==/UserScript==  

document.write('<a href="http://bing.com">Go to Bing</a> '); 

誰能幫助我?

+2

你真的想在Google主頁的底部添加一個鏈接? – ascanio 2011-05-13 16:17:52

+2

我發現這有點有趣,並意味着谷歌同時大聲笑 – tradyblix 2011-05-13 16:18:36

+0

無論如何,我張貼了一個答案... :) – ascanio 2011-05-13 16:19:54

回答

4

對於document.write可能已經太晚了。嘗試將元素添加到DOM。

var oNewA = document.createElement("a"); 
oNewA.setAttribute('href', 'http://bing.com'); 
var oText = document.createTextNode("Go to Bing"); 
oNewA.appendChild(oText); 
document.body.appendChild(oNewA); 
1

使用DOM

var link = document.createElement("a"); 
link.href="http://bing.com"; 
link.innerHTML="Go to Bing" 
document.body.appendChild(link); 

爲了更有效的使用,你可以這樣做:

var link = document.createElement("a"); 
link.href="http://bing.com"; 
link.target="_blank"; 
link.onclick=function() { 
    this.href="http://www.bing.com/search?q="+escape(document.getElementsByName("q")[0].value); 
} 
link.innerHTML="Do the same search in Bing" 
document.body.appendChild(link); 
相關問題