简体   繁体   English

通过jQuery检查属性href

[英]Check attribute href by jQuery

Let my site be http://cars.com 让我的网站为http://cars.com

I have links on my site, to http://girls.com , http://google.com , http://happy.com , etc. 我的网站上有http://girls.com http://google.com : http://happy.com http://google.com : http://happy.com等的链接。

Here is js: 这是js:

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

So, when we click on a link, it loads html of that page. 因此,当我们单击链接时,它将加载该页面的html。

How to: 如何:

  1. get address of currently opened site? 获取当前打开的网站的地址? like $_SERVER['SERVER_NAME'] on php. 如php上的$_SERVER['SERVER_NAME']
  2. sanitize attribute href ? 清理属性href such symbols like " #, ? " should be removed 诸如“ #, ? ,?”之类的符号应删除
  3. check, if this href is an url (can be /section/images/ or just / ) to my site ( $_SERVER['SERVER_NAME'] )? 检查这个href是否是我网站( $_SERVER['SERVER_NAME'] )的网址(可以是/section/images/或只是/ )? If not, do nothing (don't want to load html of other sites). 如果不是,则不执行任何操作(不想加载其他网站的html)。

Thanks. 谢谢。

  1. Use the location.host property . 使用location.host属性
  2. Use a regex to parse the string. 使用正则表达式解析字符串。
    For example: 例如:

     var pathSection = /^[^?#]+/.exec(url)[0] 
  3. Check whether it starts with a protocol. 检查它是否以协议开头。 Use a regex like /^\\w+:/ . 使用正则表达式,例如/^\\w+:/
    If you have absolute URLs to locations in your domain, you'll need to check for them separately. 如果您的网域中有绝对网址,则需要单独检查它们。

this is not possible for security reasons the restriction is called Same Origin Policy or short SOP. 出于安全原因,这是不可能的,该限制称为“ 相同来源策略”或简称SOP。 You can bypass this by proxying the request via a server side script. 您可以通过服务器端脚本代理请求来绕过此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM