根据location.host和cookie替换页面上的链接

时间:2011-10-21 20:18:57

标签: javascript jquery

我正在使用jquery重写页面上的链接列表。如果location.host不是供应商location.host并且cookie未设置为特定值,则它将找到链接并将其重写为备用值。我使用的代码在FF中运行良好,但在IE7中运行不正常。请帮忙!

<script type="text/javascript"> 
    // link hider       
    var hostadd = location.host;
    var vendor = '172.29.132.34';
    var localaccess = 'internal.na.internal.com';
    var unlock = 'http://internal.na.internal.com/Learning/Customer_Care/navigation/newhire.html';



    // link rewriter                            
    $(document).ready (
        function style_switcher(){ 
            //if not a vendor or not accessing from lms reroute user to lms
            if (hostadd != vendor && $.cookie("unlockCookie") != unlock){
                var linkData = {
                "https://www.somesite.com":             "https://internalsite.com/something",'../Compliance/something/index.html':'../somethingelse.html'

            };
            $("a").each(function() {
                 var link = this.getAttribute("href");  // use getAttribute to get what was actualy in the page, perhaps not fully qualified
                if (linkData[link]) {
                this.href = linkData[link];
            }
            });
        }
    });     
</script>

1 个答案:

答案 0 :(得分:0)

如果你动态插入链接,你可以做的是将它们存储在数据属性中,例如data-orglink =“yourlink”,它不会被浏览器转换,然后检查它 - 以及它是否在对象数组中 - 更改href。您是否有权创建数据属性?


IE7内部链接有问题,因为它在JS可以到达链接之前将主机信息打开..

http://jsfiddle.net/Cvj8C/9/

将全部工作,但IE7。因此,如果要将JS用于此功能,则需要使用完整路径:(


你的JS中有一些错误。

但似乎工作正常?

请参阅:http://jsfiddle.net/s4XmP/

或者我错过了什么? :)