php - 通过 jQuery 检查属性 href

标签 php javascript jquery html ajax

让我的网站为 http://cars.com

我的网站上有指向 http://girls.com 的链接, http://google.com , http://happy.com

这是js:

$("#wrapper a").live('click', function()
{
    var this_url = $(this).attr("href");
    $.ajax({
        url: this_url,
        complete: function(){
            //do something
        }
    });
});

因此,当我们单击链接时,它会加载该页面的 html。

如何:

  1. 获取当前打开站点的地址?喜欢 $_SERVER['SERVER_NAME']在 PHP 上。
  2. 清理属性 href ?像“#, ?”这样的符号应该被删除
  3. 检查一下,是否是 href是我的网站 ( /section/images/ ) 的 url(可以是 / 或只是 $_SERVER['SERVER_NAME'] )?如果没有,则不执行任何操作(不想加载其他网站的 html)。

谢谢。

最佳答案

  1. 使用location.host property .
  2. 使用正则表达式解析字符串。
    例如:

    var pathSection = /^[^?#]+/.exec(url)[0]
    
  3. 检查是否以协议(protocol)开头。使用像 /^\w+:/ 这样的正则表达式。
    如果您有域中某个位置的绝对网址,则需要单独检查它们。

关于php - 通过 jQuery 检查属性 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5210910/

相关文章:

PHP - 自定义二维码?

php - 清理 Feed 聚合器的 RSS Feed 内容

javascript - 等待窗口加载事件注册服务 worker

javascript - ngRepeat 项目和重复项目的属性?

javascript - 在 jquery 中使用 $(this) 选择器和另一个嵌套选择器

javascript - 拉斐尔.JS : How drag and drop image dom elements in raphael paper

php - 使用正则表达式匹配作为数组指针

php - 是否可以将 store_result() 和 bind_result() 与 PHP PDO 一起使用?

javascript - $.jqx.dataAdapter 不是构造函数

php - 安全地转义原始 SQL 查询中的变量