来自google.com/dictionary的Ajax字查找 [英] Ajax word lookup from google.com/dictionary

查看:132
本文介绍了来自google.com/dictionary的Ajax字查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我不知道如何编写ajax或javascipt。



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



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



所以我需要一个表单,我的学生输入他们正在查找的单词,然后将该单词插入到URL中的正确位置 http://www.google.com/dictionary?aq=f&langpair=en|en& ; q = WORD INSERTED HERE& hl = en



然后结果需要显示在与搜索框相同的位置,所以我的学生不是导航到另一个页面。



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



任何帮助将不胜感激。

解决方案

不幸的是,大多数浏览器将Ajax请求从一个域限制到另一个域,所以您不能轻易地将ajax调用到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 =textid =word/>
< input type =buttonvalue =submitonclick =search(document.getElementById('word')。value)/>< br>
< a href =#onclick =toggleIframe()>切换结果< / a>< br />
< iframe id =searchResultwidth =500height =200>< / iframe>


caveat: I do not know how to write ajax or javascipt.

I am wanting to use the dictionary from google.com/dictionary on my school website. Much like the custom search engine available from Google.

I figure the easiest way is to use the URL and pass in the word as a parameter and have the results populated in a div located below the search box.

So I need a form where my students type in the word they are looking up and then that word is inserted into the proper location in the URL http://www.google.com/dictionary?aq=f&langpair=en|en&q=WORD INSERTED HERE&hl=en

Then the results need to be displayed in the same location as the search box, so my students are not navigating to another page.

Can this be done? If so how?

Any help would be appreciated.

解决方案

Unfortunately most browsers restrict Ajax requests from one domain to another, so you can't easily make an ajax call to google.com for example.

If you're open to using an iframe, the below code might do want you want. It's just not quite as pretty.

<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>

这篇关于来自google.com/dictionary的Ajax字查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆