如何使用jquery替换window.open中的url,具体取决于country ip

时间:2013-09-19 10:55:15

标签: javascript jquery

我在这里遇到了问题,我需要一些帮助。我正在使用一个名为我网站聊天的聊天服务。我为美国设置了第二个帐户,我想要的是根据IP地址替换聊天的onclick网址。我正在使用来自“Maxmind”的脚本来检测ip我还没有找到替换那里的url的方法。

所以我得到的代码是:

<img src="http://static.ssl7.net/b/en/c3c88152ba6c7e56746dc864606b/1.gif" style="cursor:     pointer; border: none;" alt="Helpdesk Software" onclick="window.open('http://ssl7.net/chat/en/c3c88152ba6c7e56746dc864606b/'+document.location.href,'','height=400,width=300,menubar=no, location=no,resizable=yes,scrollbars=no,status=yes');">

我需要把它变成这个

<img src="http://static.ssl7.net/b/en/b5695d38e0f3db83ebf705147155/8.gif" style="position:fixed;top:170px;right:0;z-index:9999;cursor:pointer;border:none;" alt="Live Support Chat" onclick="window.open('http://ssl7.net/chat/en/b5695d38e0f3db83ebf705147155/'+document.location.href,'','height=400,width=300,menubar=no, location=no,resizable=yes,scrollbars=no,status=yes');">

所以基本上唯一改变的是网址。 我试图用jquery替换它,但我不能让它工作,请不要phone[country]检测到ip。

j('img').each(function() {
  var href = $(this).attr('onclick');
  j(this).click(function() { window.open = 'http://ssl7.net/chat/en/b5695d38e0f3db83ebf705147155/mecmesin.com'; })
         .removeAttr('onclick' , phone[country]);
}, );

非常感谢您的回复。

1 个答案:

答案 0 :(得分:1)

首先,我用jQuery捕获ssl7的两个图像元素。在此之后,我会浏览每一个并用RegEx替换onclick-Attr。

希望这会有所帮助。祝你有愉快的一天。

var newCode = 'b5695d38e0f3db83ebf705147155';
jQuery('img[onclick]').each(function() {
   var sOnClick = jQuery(this).attr('onclick');
   jQuery(this).attr('onclick', sOnClick.replace(/\/(.{28})\//, '/'+newCode+'/'));
});