【问题标题】:How to prevent storing domain name in href = $('a#myId').attr('href'); in IE7 [duplicate]如何防止在 href = $('a#myId').attr('href'); 中存储域名在 IE7 [重复]
【发布时间】:2012-01-29 17:56:49
【问题描述】:

例如如果

location.href = 'http://mydomain.com/en/'

我有

<a href="my-file.html" id="myId">i am just a link</a>

所以

href = $('a#id').attr('href');

由于某种原因,Firefox、Chrome 和 Opera 返回:my-file.html

但 IE7 会返回:http://mydomain.com/en/my-file.html

我用域名尝试了这个功能,但报错:

function str_replace(busca, repla, orig)
{
    str     = new String(orig);
    rExp    = "/"+busca+"/g";
    rExp    = eval(rExp);
    newS    = String(repla);
    str = new String(str.replace(rExp, newS));
    return str;
}

domain-name is not defined
[Detener en este error] rExp = eval(rExp); 

关于如何预防的任何想法???

【问题讨论】:

  • 你的 str_replace 函数和 eval 真的很可怕 - 从使用 eval 到泄漏全局变量的多种方式。
  • 请告诉你,你到底想达到什么目的?
  • @Umex 我只需要 firefox、chrome 和 opera 行为
  • 查看how to create a regex instance with new RegExp()。 (不,它不会在这里解决您的问题,但是eval() - 真的吗?)
  • 我实际上并不想使用那个函数,我只是尝试使用那个函数来处理简单的字符串,而且总是很好用!

标签: javascript jquery internet-explorer-7


【解决方案1】:

尝试以下:

//this will give you filename only var chk = "http://mydomain.com/en/test.html"; var chkArr = chk.split("/"); var filenameOnly = chkArr.pop();

希望对你有帮助

【讨论】:

    【解决方案2】:

    您应该避免在代码中使用 eval。

    您可以使用str = str.replace(/.*\//, ''); 去除字符串中最后一个/ 之前的所有内容。

    【讨论】:

      猜你喜欢
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多