我如何捕获点击的网址? [英] How can I capture the url clicked?

查看:92
本文介绍了我如何捕获点击的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器通过将一个函数* file_get_contents *的结果回显到一个URL来加载一个html页面。我想在此之后获取用户点击的网址。我试过:

I've got a server that loads an html page by echoing the result of a function *file_get_contents* to an URL. I want to get the URL that is clicked by the user after this. I've tried:

$result = file_get_contents('http://www.google.com/');

header('Content-Type: text/html; charset=iso-8859-1');

echo $result;

echo '<script type="text/javascript">',
  "Event.observe(document.body, 'click', function(event) {",
  'alert("hi");',
  '});</script>';

但我不知道为什么它不起作用!

But I don't know why it doesn't work!

谢谢

Thank you

推荐答案

您需要一个如下所示的脚本:

You want a script that looks like this:

(function() {
  function onclick(event) {
    event = event || window.event;
    var target = event.target || event.srcElement;
    if (target.tagName && target.tagName.toLowerCase() === 'a') {
      alert(target.href);
    }
  }

  if (document.body.addEventListener) {
    document.body.addEventListener('click', onclick, false);
  } else if (document.body.attachEvent) {
    document.body.attachEvent('onclick', onclick);
  }
})();

这可以在IE和其他浏览器中使用,无需使用任何JS库。

This will work in IE and other browsers without using any JS libraries.

这篇关于我如何捕获点击的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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