更改URL的一部分并根据特定字符jQuery附加URL

问题描述

|| 我需要查看页面中的href,并查看URL中是否包含某些文本,如果需要的话,我需要将其更改为其他内容,然后在末尾附加一个位置。 如果链接包含\“ ice / app?service = page&page = probrowse \” 例如:http://www.google.com/ice/app?service=page&page=probrowse&cat=1&year=2011 我需要将其更改为\“ club / probrowse.htm?” 例如:http://www.google.com/club/probrowse.htm?cat=1&year=2011 我目前正在运行下面考虑的脚本?网址中的&,并在href后面附加一个位置。
<script type=\"text/javascript\">
    jQuery(document).ready(function(){  
        jQuery(\'a\').attr(\'href\',function() {
                return (this.href.indexOf(\"?\") >= 0) ? this.href + \"&location=/abc/123\" : this.href + \"?location=/abc/123\";
           });

    });
</script>
由于我正在从另一个站点获取信息,并且需要进行一些小的URL更改,因此遇到了其他麻烦。 总体目标是改变 google.com/ice/app?service=page&page=probrowse&cat=1&year=2011 进入 google.com/club/probrowse.htm?cat=1&year=2011&location=/abc/123     

解决方法

        我相信这是您要寻找的:
<script type=\"text/javascript\">
jQuery.noConflict();
jQuery(function(){
    var substr = \'ice/app?service=page&page=probrowse\';
    var newstring = \'club/probrowse.htm?\';
    jQuery(\"a\").each(function(){
        if(this.href.indexOf(substr) != -1){
            var newHref = this.href;
            newHref = newHref.replace(substr,newstring);
            newHref += \'&location=/abc/123\';
            newHref = newHref.replace(\'?&\',\'?\');
            this.href = newHref;
        }
    });
});
</script>
    ,        
<script type=\"text/javascript\">
    jQuery.noConflict();
    jQuery(function(){
        var substr = \'ice/app?service=page&page=probrowse\';
        var newstring = \'club/probrowse.htm?\';
        jQuery(\"a\").attr(\'href\',function(){
            if(this.href.indexOf(substr) != -1){
                var newHref = this.href;
                newHref = newHref.replace(substr,newstring);
                newHref += \'&location=/abc/123\';
                newHref = newHref.replace(\'?&\',\'?\');
                this.href = newHref;
} else {
        return (this.href.indexOf(\"?\") >= 0) ? this.href + \"&location=/abc/123\" : this.href + \"?location=/abc/123\"; 
    }

        });
    });
    </script>