0

http://jsfiddle.net/dS4r3/24/?q=hsbc

这段代码的作用是获取页面的引用者,如果它包含您在那里看到的关键字之一,它会填充特定 div 的内容(它也显示它)

正如您所看到的,当您单击此处并显示 div 并填充包含关键字“hsbc”的内容时,它工作正常。简而言之,函数正在执行。

现在的问题是当我在这里实现完全相同的代码时,该函数似乎没有执行:http ://segurosendirecto.com.ar/cotizador-de-seguros-auto/?kw=hsbc

到目前为止我尝试过但没有奏效的事情:

  • 更新jQuery
  • 删除所有其他脚本只是为了确保它与这个不冲突。

为什么它在第二个链接上不起作用?

4

3 回答 3

1

页面的引用者是启动链接的页面;在这种情况下,从 stackoverflow 中单击上面的 jsfiddle 链接,jsfiddle 中的引用者将是http://stackoverflow.com/questions/9695194/whats-wrong-with-this-js-code-implemented-in-this-context

jsfiddle 在一个框架内运行,因此您将子框架的引用者作为父框架;这就是为什么您的示例测试在 jsfiddle 中工作的原因。我认为您需要使用或 top.document.referrer 或 parent.document.referrer 来获取 stackoverflow url 作为将您引荐到 jsfiddle 页面的内容(如果它不破坏 js 跨站点安全限制)

如果您想知道当前页面的 url 是什么(例如在第二个链接的上下文中),您可以使用 document.location.href http://segurosendirecto.com.ar/cotizador-de-seguros-auto/?kw=hsbc而不是 document.referrer。然后你会在 url 的末尾找到 hsbc。我认为 document.location.href 可能是您希望在实际站点中获取 hsbc 参数的内容。

于 2012-03-14T03:14:44.627 回答
1

jsfiddle 不会正常处理引荐来源网址,因此在这种情况下您从 jsfiddle 学到的内容不会转移到您的网站。您的网站正在正常处理引荐来源网址,而引荐来源网址 ( http://stackoverflow.com/questions/9695194/whats-wrong-with-this-js-code-implemented-in-this-context) 根本不包含字符串hsbc

您需要做的是new-page在您的网站上创建一个新页面(例如 ),其中包含指向 的链接cotizador-de-seguros-auto,访问new-page/?kw=hsbc并单击新链接。那么你的问题就会消失。

于 2012-03-14T03:09:00.907 回答
0

Your code is not executing properly. Wrap all of your code with:

$(function(){

});

Then move the entire <script></script> block to the head.

A more preferred way is to add that code you your already existing document-ready statement, found in the head tags.

$(document).ready(function () {

See here for more information.

于 2012-03-14T03:32:39.137 回答