2009-12-17 58 views
0

告诫:我不知道如何编写ajax或javascipt。来自google.com/dictionary的Ajax单词查询

我想在我的学校网站上使用google.com/dictionary上的字典。很像Google提供的自定义搜索引擎。

我认为最简单的方法是使用URL并传入单词作为参数,并将结果填充到位于搜索框下方的div中。

所以我需要在我的学生在单词键入一个形式,他们正在寻找了,然后这个词被插入到适当的位置在INSERTED这里& HL = EN

那么结果需要的URL http://www.google.com/dictionary?aq=f&langpair=en|en&q=WORD显示在与搜索框相同的位置,所以我的学生不会导航到其他页面。

可以这样做吗? 如果是这样怎么样?

任何帮助,将不胜感激。

回答

1

不幸的是,大多数浏览器都会限制Ajax从一个域到另一个域的请求,因此您不能轻易地向aaa调用google.com。

如果你打算使用iframe,下面的代码可能会希望你想要的。它只是不太漂亮。

<script> 

function search(word) { 
    var url = "http://www.google.com/dictionary?aq=f&langpair=en|en&q=" + word; 
    document.getElementById("searchResult").src = url; 
    showIframe(); 
} 
function showIframe() { 
    document.getElementById("searchResult").style.display = ""; 
} 

function toggleIframe() { 
    var display = document.getElementById("searchResult").style.display; 
    if (display == "none") { 
     display = ""; 
    } else { 
     display = "none" 
    } 
    document.getElementById("searchResult").style.display = display; 
} 
</script> 

<input type="text" id="word"/> 
<input type="button" value="submit" onclick="search(document.getElementById('word').value)"/><br> 
<a href="#" onclick="toggleIframe()">Toggle results</a><br/> 
<iframe id="searchResult" width="500" height="200"></iframe> 
+0

这正是我想要的,并没有那么漂亮。任何方式来在iframe中找出结果。我有一种感觉,消除品牌,这是违反规则。我真正喜欢的是只是定义的结果。 非常感谢您的脚本,这是一个很好的起点。 – 2009-12-17 20:59:26