Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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将url参数附加到所有传出请求? 背景_Javascript_Jquery - Fatal编程技术网

如何使用javascript将url参数附加到所有传出请求? 背景

如何使用javascript将url参数附加到所有传出请求? 背景,javascript,jquery,Javascript,Jquery,我的登录页上有客户,我希望将url参数(如aff_id=X附加到所有传出流量) 目前,我有这样的想法: $('a').each(function(i, node) { if (node.href != '#') { $(node).addClass('outgoing'); } }); $('.outgoing').attr('href', function(i, h) { if (h) { var arg_str = 's4=' + aff_id +

我的登录页上有客户,我希望将url参数(如
aff_id=X
附加到所有传出流量)

目前,我有这样的想法:

$('a').each(function(i, node) {
  if (node.href != '#') {
    $(node).addClass('outgoing');
  }
});

$('.outgoing').attr('href', function(i, h) {
    if (h) {
        var arg_str = 's4=' + aff_id + '&s5=' + uuid;
        return h + (h.indexOf('?') != -1 ? "&" + arg_str : "?" + arg_str);
    }
});
问题是,它不能处理所有发出的请求,例如表单提交

  • 是否可以通过javascript附加侦听器,将url参数附加到所有传出流量
  • 是否有更干净的方法将url参数附加到我的登录页上的传出流量

  • 假设您使用的是jQuery:

    var uuid = '',
        aff_id = '';
    $('a, form').each(function() {
        var self = $(this);
        var qstr = 's4=' + aff_id + '&s5=' + uuid;
        if ('a' === self.get(0).tagName.toLowerCase()) {
            var href = self.attr('href') || '';
            if (!href.match(/^#/)) {
                self.attr('href', href + (href.indexOf('?') > -1 ? '?' : '&') + qstr);
            }
        } else {
            var action = self.attr('action') || '';
            if (!action.match(/^#/)) {
                self.attr('action', action + (action.indexOf('?') > -1 ? '?' : '&') + qstr);
            }
        }
    });
    

    可以作为一个简单的快速解决方案。

    假设您使用的是jQuery:

    var uuid = '',
        aff_id = '';
    $('a, form').each(function() {
        var self = $(this);
        var qstr = 's4=' + aff_id + '&s5=' + uuid;
        if ('a' === self.get(0).tagName.toLowerCase()) {
            var href = self.attr('href') || '';
            if (!href.match(/^#/)) {
                self.attr('href', href + (href.indexOf('?') > -1 ? '?' : '&') + qstr);
            }
        } else {
            var action = self.attr('action') || '';
            if (!action.match(/^#/)) {
                self.attr('action', action + (action.indexOf('?') > -1 ? '?' : '&') + qstr);
            }
        }
    });
    

    可以作为一个简单的快速解决方案。

    我想你可以用一个服务人员。稍后我将尝试编写一个示例。我认为您可以使用服务人员。稍后我将试着写一个例子。谢谢你,我感谢你的回答。然而,这是专门为表单设计的,我正在寻找更一般的东西。在某些情况下,
    window.href
    可能直接由javascript修改,这也将绕过此问题。谢谢,我感谢您的回答。然而,这是专门为表单设计的,我正在寻找更一般的东西。在某些情况下,
    window.href
    可能直接由javascript修改,这也将绕过此操作。