让 Javascript 在新窗口中省略外部链接脚本的锚标记
我已经把头撞在墙上一段时间了,因为我知道答案很简单,但它却让我困惑。我有一些编程经验,但主要是 PHP。我遇到过一个实例,我正在处理的网站需要一个脚本来自动检测外部链接并在新窗口中打开它们。我发现了 2 个独立的 javascript 代码,它们工作得很好,但问题是有一个锚标记,例如 href="#B",代码认为这是一个外部链接。我已经做了大量研究并尝试执行 indexOf 和 endWith() 函数,但都没有忽略具有#letter(即#B)的链接。
如果我能得到一些进一步的帮助或指点,那就太好了。请参阅我在网上找到的下面的代码:
assigntarget:function(){
var rexcludedomains=new RegExp(this.excludedomains.join("|"), "i")
var all_links=document.getElementsByTagName("a")
if (this.mode=="auto" || (this.mode=="manual" && this.togglebox.checked)){
for (var i=0; i<=(all_links.length-1); i++){
if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("http:")!=-1)
all_links[i].target=ddwindowlinks.linktarget
}
}
else{
for (var i=0; i<=(all_links.length-1); i++)
all_links[i].target=""
}
现在我尝试使用上面提到的索引来查找链接中是否存在#,如下所示:
for (var i=0; i<=(all_links.length-1); i++){
if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("#") ==-1 && all_links[i].href.indexOf("http://") !=-1)
all_links[i].target=ddwindowlinks.linktarget
感谢您的任何帮助或potiners。
I've been beating my head against the wall for a bit as I know the answer is simple but it eludes me. I have some programming experience but mainly with PHP. I came across an instance in which a site I am working on needed a script to automatically detect external links and open them in a new window. I found 2 separate javascript codes and they work beautifully but the problem is where there is an anchor tag such as href="#B" the code considers this an external link. I've done significant research and tried doing an indexOf and endWith() functions but neither are ignoring a link that has a #letter (i.e. #B).
Would be great if I could get some further assistance or pointers. See the code below I found on the net:
assigntarget:function(){
var rexcludedomains=new RegExp(this.excludedomains.join("|"), "i")
var all_links=document.getElementsByTagName("a")
if (this.mode=="auto" || (this.mode=="manual" && this.togglebox.checked)){
for (var i=0; i<=(all_links.length-1); i++){
if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("http:")!=-1)
all_links[i].target=ddwindowlinks.linktarget
}
}
else{
for (var i=0; i<=(all_links.length-1); i++)
all_links[i].target=""
}
Now I've tried using index as I mentioned above to find if # exists in the link as below:
for (var i=0; i<=(all_links.length-1); i++){
if (all_links[i].hostname.search(rexcludedomains)==-1 && all_links[i].href.indexOf("#") ==-1 && all_links[i].href.indexOf("http://") !=-1)
all_links[i].target=ddwindowlinks.linktarget
Thanks for any help or potiners.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以删除锚标记
并对其进行测试。不过,我有兴趣了解
this.excludedomains.join("|")
到底等于什么,以及它失败的确切链接。You could always just strip out the anchor tag
and test against that. I'd be interested to see exactly what
this.excludedomains.join("|")
equals and which exact links it fails against, though.