简体   繁体   中英

Check attribute href by jQuery

Let my site be http://cars.com

I have links on my site, to http://girls.com , http://google.com , http://happy.com , etc.

Here is 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.

How to:

  1. get address of currently opened site? like $_SERVER['SERVER_NAME'] on php.
  2. sanitize attribute 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'] )? If not, do nothing (don't want to load html of other sites).

Thanks.

  1. Use the location.host property .
  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+:/ .
    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. You can bypass this by proxying the request via a server side script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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