网站如何突出显示您在搜索引擎中使用的搜索词? [英] How does a website highlight search terms you used in the search engine?

查看:88
本文介绍了网站如何突出显示您在搜索引擎中使用的搜索词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一些网站突出显示您使用的搜索引擎关键字来访问该页面. (例如您在Google搜索列表中键入的关键字)

I've seen some websites highlight the search engine keywords you used, to reach the page. (such as the keywords you typed in the Google search listing)

它如何知道您在搜索引擎中键入了哪些关键字?它是否检查引荐来源网址HTTP标头或其他内容?有可用的脚本可以做到这一点吗?我不确定是服务器端还是JavaScript.

How does it know what keywords you typed in the search engine? Does it examine the referrer HTTP header or something? Any available scripts that can do this? It might be server-side or JavaScript, I'm not sure.

推荐答案

这可以在服务器端或客户端完成.搜索关键字是通过查看HTTP Referer(sic)标头确定的.在JavaScript中,您可以查看document.referrer.

This can be done either server-side or client-side. The search keywords are determined by looking at the HTTP Referer (sic) header. In JavaScript you can look at document.referrer.

有了引荐来源网址后,您将检查它是否是您所了解的搜索引擎结果页面,然后解析出搜索词.

Once you have the referrer, you check to see if it's a search engine results page you know about, and then parse out the search terms.

例如,Google的搜索结果具有如下网址:

For example, Google's search results have URLs that look like this:

http://www.google.com/search?hl=en&q=programming+questions

q查询参数是搜索查询,因此您希望将其提取出来并取消URL的转义,结果是:

The q query parameter is the search query, so you'd want to pull that out and un-URL-escape it, resulting in:

programming questions

然后,您可以在页面上搜索术语,并根据需要突出显示它们.如果您要在服务器端进行操作,则需要先修改HTML,然后再将其发送给客户端.如果您是在客户端进行操作,则可以操作DOM.

Then you can search for the terms on your page and highlight them as necessary. If you're doing this server side-you'd modify the HTML before sending it to the client. If you're doing it client-side you'd manipulate the DOM.

现有的库可以为您完成此操作,例如此库.

There are existing libraries that can do this for you, like this one.

这篇关于网站如何突出显示您在搜索引擎中使用的搜索词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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