Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用.attr(“href”)前面的参数添加当前url?_Javascript_Jquery - Fatal编程技术网

Javascript 如何使用.attr(“href”)前面的参数添加当前url?

Javascript 如何使用.attr(“href”)前面的参数添加当前url?,javascript,jquery,Javascript,Jquery,我有外部链接 我需要添加:http://myurl.com?url=在外部链接的href之前 使之成为 我当前的js代码: $.expr[':'].external = function(obj){ return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname); }; $('a:external').addClass('external').

我有外部链接
我需要添加:
http://myurl.com?url=
在外部链接的href之前 使之成为 我当前的js代码:

  $.expr[':'].external = function(obj){
      return !obj.href.match(/^mailto\:/)
              && (obj.hostname != location.hostname);
  };
  $('a:external').addClass('external').attr("href", window.location+("href"));
它不起作用。我猜这是语法

我对jquery还比较陌生,所以我更喜欢更脏/直接的方法

干杯

具有以下功能:

$("a[href^='http:']:not([href*='"+window.location.host+"'])").each(function(){
  $(this)
    .attr("href","http://mysite.com?url="+$(this).attr("href"))
    .addClass("external");
});
此HTML:

<a href="http://www.google.com">Google</a>
<a href="http://localhost/somepage.html">Localhost</a>

变成这样:

<a class="external" href="http://mysite.com?url=http://www.google.com">Google</a>
<a href="http://localhost/somepage.html">Localhost</a>

具有以下内容:

$("a[href^='http:']:not([href*='"+window.location.host+"'])").each(function(){
  $(this)
    .attr("href","http://mysite.com?url="+$(this).attr("href"))
    .addClass("external");
});
此HTML:

<a href="http://www.google.com">Google</a>
<a href="http://localhost/somepage.html">Localhost</a>

变成这样:

<a class="external" href="http://mysite.com?url=http://www.google.com">Google</a>
<a href="http://localhost/somepage.html">Localhost</a>

尝试以下操作,在将URL附加到网关URL之前,请注意
转义
该URL与更多URL匹配,并且略微优化:

var externalLinkPattern = new RegExp("^http://" + window.location.host, "i"),
    gateway             = "http://myurl.com/current/page?url=";

$("a[href^='http:']").each(function() {
    if ( !externalLinkPattern.test(this.href) ) {
        this.href = gateway + escape(this.href);
        $(this).addClass("external");
    }
});

请尝试以下操作,在将URL附加到网关URL之前,请注意
转义
该URL与更多URL匹配,并稍微优化:

var externalLinkPattern = new RegExp("^http://" + window.location.host, "i"),
    gateway             = "http://myurl.com/current/page?url=";

$("a[href^='http:']").each(function() {
    if ( !externalLinkPattern.test(this.href) ) {
        this.href = gateway + escape(this.href);
        $(this).addClass("external");
    }
});

下面是我们在
insightcruises.com
上使用的内容,它将所有出站URL重写为前缀
http://insightcruises.com/cgi/go/

jQuery(function ($) {
        $('a[href^=http:]').each(function () {
                var $this = $(this);
                var href = $this.attr('href');
                href = href.replace(/#/, '%23');
                var newhref = 'http://insightcruises.com/cgi/go/'+href;
                // $('<p />').text(newhref).appendTo('body');                   
                $this.attr('href', newhref);
            });
    });
window.cgigo = function (url, windowName, windowFeatures) {
    if (url.match(/^http:/)) {
        url = url.replace(/\#/, '%23');
        url = 'http://insightcruises.com/cgi/go/'+url;
    };
    window.open(url, windowName, windowFeatures);
};
jQuery(函数($){
$('a[href^=http:')。每个(函数(){
var$this=$(this);
var href=$this.attr('href');
href=href.replace(/#/,“%23”);
var newhref=http://insightcruises.com/cgi/go/'+href;
//$('

').text(newhref.appendTo('body'); $this.attr('href',newhref); }); }); window.cgigo=函数(url、windowName、windowFeatures){ if(url.match(/^http:/)){ url=url.replace(/\ \ \/,“%23”); url='1〕http://insightcruises.com/cgi/go/“+url; }; 打开(url、windowName、windowFeatures); };


以下是我们在
insightcruises.com
上使用的内容,它将所有出站URL重写为前缀
http://insightcruises.com/cgi/go/

jQuery(function ($) {
        $('a[href^=http:]').each(function () {
                var $this = $(this);
                var href = $this.attr('href');
                href = href.replace(/#/, '%23');
                var newhref = 'http://insightcruises.com/cgi/go/'+href;
                // $('<p />').text(newhref).appendTo('body');                   
                $this.attr('href', newhref);
            });
    });
window.cgigo = function (url, windowName, windowFeatures) {
    if (url.match(/^http:/)) {
        url = url.replace(/\#/, '%23');
        url = 'http://insightcruises.com/cgi/go/'+url;
    };
    window.open(url, windowName, windowFeatures);
};
jQuery(函数($){
$('a[href^=http:')。每个(函数(){
var$this=$(this);
var href=$this.attr('href');
href=href.replace(/#/,“%23”);
var newhref=http://insightcruises.com/cgi/go/'+href;
//$('

').text(newhref.appendTo('body'); $this.attr('href',newhref); }); }); window.cgigo=函数(url、windowName、windowFeatures){ if(url.match(/^http:/)){ url=url.replace(/\ \ \/,“%23”); url='1〕http://insightcruises.com/cgi/go/“+url; }; 打开(url、windowName、windowFeatures); };


您是jQuery新手,并且已经在使用自定义选择器扩展
$.expr
?好极了@罗吉:经过一些修改,我觉得下面有一个令人满意的解决方案。@Crescent Fresh:我从其他地方取了前四行。我不得不承认我一点也不理解他们@乔纳森·桑普森:谢谢,你的回答很好。我选择了Justin的答案来回答我的问题,因为我可以使用var“gateway”并从window.location中提取url。谢谢大家的回答,非常感谢。从现在起,你们将在堆栈中闲逛,你们真是太棒了=)你们是jQuery的新手,并且已经用自定义选择器扩展了
$.expr
?好极了@罗吉:经过一些修改,我觉得下面有一个令人满意的解决方案。@Crescent Fresh:我从其他地方取了前四行。我不得不承认我一点也不理解他们@乔纳森·桑普森:谢谢,你的回答很好。我选择了Justin的答案来回答我的问题,因为我可以使用var“gateway”并从window.location中提取url。谢谢大家的回答,非常感谢。将从现在开始在堆栈中徘徊,你们真是太棒了=)这缺少了一些可能的链接。这缺少了一些可能的链接。