Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 新窗口中的非站点链接脚本,不收集页面中的所有链接_Javascript_Html - Fatal编程技术网

Javascript 新窗口中的非站点链接脚本,不收集页面中的所有链接

Javascript 新窗口中的非站点链接脚本,不收集页面中的所有链接,javascript,html,Javascript,Html,嗨,我是个初学者,这是我的第一个问题 我最近建立了一个网站,有一些外部链接。默认情况下,所有链接都设置为在同一窗口中打开,但希望让访问者选择切换外部链接以在新窗口中打开 我从以下方面找到了很棒的脚本: 编辑 以下是dynamicdrive中的脚本: //Open offsite links in new window script- http://www.dynamicdrive.com/ //Created: August 28th, 2007' var ddwindowlinks={ //

嗨,我是个初学者,这是我的第一个问题

我最近建立了一个网站,有一些外部链接。默认情况下,所有链接都设置为在同一窗口中打开,但希望让访问者选择切换外部链接以在新窗口中打开

我从以下方面找到了很棒的脚本:

编辑 以下是dynamicdrive中的脚本:

//Open offsite links in new window script- http://www.dynamicdrive.com/
//Created: August 28th, 2007'

var ddwindowlinks={
//1)Enter domains to be EXCLUDED from opening in new window:
excludedomains: ["dynamicdrive.com", "google.com"],

//2) Target for links that should open in a new window (ie: "_blank", "secwin" etc):
linktarget: "_blank",

//3) Specify operating mode ("auto" or "manual"):
mode: "manual",

//4) If mode is "manual", customize checkbox HTML to show to users (Preserve id attribute):
toggleHTML: '<form><input type="checkbox" id="targetcheckbox" checked="checked" /><label for="targetcheckbox">Open off-site links in new window?</label></form>',

//5) If mode is "manual", enable user persistence so the state of the checkbox is remembered?
persist: true,

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=""
    }
    if (this.mode=="manual" && this.persist)
        this.setCookie("dlinktarget", (this.togglebox.checked)? "yes" : "no", 30) //remember user setting for 30 days (set to -1 then reload page to erase cookie)
},

init:function(){
    if (document.getElementById && this.mode=="manual"){
        document.write(this.toggleHTML)
        this.togglebox=document.getElementById("targetcheckbox")
        this.togglebox.onclick=function(){ddwindowlinks.assigntarget()}
        if (this.persist && this.getCookie("dlinktarget")!="")
            this.togglebox.checked=(this.getCookie("dlinktarget")=="yes")? true : false
    }
    if (window.addEventListener)
        window.addEventListener("load", function(){ddwindowlinks.assigntarget()}, false)
    else if (window.attachEvent)
        window.attachEvent("onload", function(){ddwindowlinks.assigntarget()})
},

getCookie:function(Name){
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
        return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
},

setCookie:function(name, value, days){
    var expireDate = new Date()
    //set "expstring" to either an explicit date (past or future)
        var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
        document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"
}

}

ddwindowlinks.init()
//在新窗口脚本中打开异地链接-http://www.dynamicdrive.com/
//创建日期:2007年8月28日'
var窗口链接={
//1) 输入要排除在新窗口中打开的域:
排除域名:[“dynamicdrive.com”、“google.com”],
//2) 应在新窗口中打开的链接的目标(即:“\u blank”、“secwin”等):
链接目标:“_blank”,
//3) 指定操作模式(“自动”或“手动”):
模式:“手动”,
//4) 如果模式为“手动”,则自定义复选框HTML以向用户显示(保留id属性):
toggleHTML:“在新窗口中打开非站点链接?”,
//5) 如果模式为“手动”,是否启用用户持久性以便记住复选框的状态?
坚持:对,,
assigntarget:function(){
var rexcludedomains=new RegExp(this.excludeddomains.join(“|”),“i”)
var all_links=document.getElementsByTagName(“a”)
if(this.mode==“auto”| |(this.mode==“manual”&&this.togglebox.checked)){

对于(var i=0;i而言,自己编写代码可能更简洁。请尝试以下操作:

// Grab every <a> tag in the document.
var allTheTags = document.getElementsByTagName('a');

function parseTags(tags) {
    var size = tags.length; // cache the size;

    for (var i = 0; i < size; i++) {
        var tag = tags[i];
        var href = tag.getAttribute('href');
        // Do we have a target attribute? (and, of course, an href attribute)
        if (href && !tag.getAttribute('target')) {
              var ourHostName = window.location.hostname;
              href = href.split('://');
              // Is there a protocol?
              if (href.length > 1) {
                  href = href[1].split('/')[0]; // Get everything before the first /
                  if (href != window.location.hostname &&
                      href != 'www' + window.location.hostname) {
                      // Sometimes, hostname does not have www in it.
                      tag.setAttribute('target', '_blank');
                  }
              }
        }
    }
};

// Call our function.
parseTags(allTheTags);

这样,在我们的标记被注入后,这个悬停事件是下一个要执行的在线事件,现在将看到在DOM中生成的poproll标记。

我通过编辑poprol的代码解决了我的问题,将href更改为:

<a href="'+href+'" target="_blank">


Hi Graham,谢谢你的快速回答,我刚刚尝试了你建议的js,不幸的是问题仍然存在。仍然可以处理文本链接,而不处理滚动。你有什么其他建议吗?嗯,这肯定可以。弹出窗口的标记是由JavaScript生成的,以后添加到DOM中吗?我想是的。poproll也有jquery js,你想让我从中粘贴代码吗?最肯定的是。正如我在上面的回答中所说的,如果你能听到呈现弹出窗口的事件,你可以再次运行parseTags函数,因为该div中的a标记现在将成为DOM的一部分。Thnx m8,我为自己是noob而道歉,但是您制作的do I在您建议的脚本末尾插入,以及“SomeElementYouUse for PopRoll”我是否要用我用于翻滚的jquery的名称来更改此内容?再次感谢您对我的无知。我刚刚看到您的评论。很抱歉。我重新编辑了原始帖子并加入了原始纸条,尽管Graham Robertson建议的脚本也适用于此。
(function($){jQuery.fn.poproll=function(settings){var m_bHovering=false;var m_nDivId=0;
var $m_ImageDiv=null;var $m_TextDiv=null;
var eOptions={Img:0,ImgPosX:1,ImgPosY:2,Txt:3,TxtPosX:4,TxtPosY:5,TxtCol:6,TxtFont:7,TxtSize:8,TxtItallic:9,TxtBold:10,TxtBkgrndCol:11,TxtWidth:12};
function ClosePopup(){if($m_ImageDiv!==null){$m_ImageDiv.remove();
$m_ImageDiv=null;if($m_TextDiv!==null){$m_TextDiv.remove();
$m_TextDiv=null}}}function HoverOver(div){m_bHovering=true;var nDivId=$(div).attr('id');
if(nDivId!==m_nDivId){m_nDivId=nDivId;ClosePopup()}if($m_ImageDiv===null){var anchor=$(div).find('a');
var optionArray=$(anchor).attr('rev').split('~#~');
var href=$(anchor).attr('href');
if(href===undefined){$(div).append('<div id="poproll_img" style="position:absolute; left:'+optionArray[eOptions.ImgPosX]+'px; top:'+optionArray[eOptions.ImgPosY]+'px; z-index:100;"><img src="'+optionArray[eOptions.Img]+'" name="popup_roll_2" alt="" style="position:absolute;left:0px;top:0px;"></div>')}else{$(div).append('<div id="poproll_img" style="position:absolute; left:'+optionArray[eOptions.ImgPosX]+'px; top:'+optionArray[eOptions.ImgPosY]+'px; z-index:100;"><a href='+href+'><img src="'+optionArray[eOptions.Img]+'" name="popup_roll_2" alt="" border="0" style="position:absolute;left:0px;top:0px;"></a></div>')}$m_ImageDiv=$('#poproll_img');
if(optionArray[eOptions.Txt].length>0){var fontStyle=optionArray[eOptions.TxtItallic]>0?'italic':'normal';
var fontWeight=optionArray[eOptions.TxtBold]>0?'bold':'normal';$(div).append('<div id="poproll_txt" style="position:absolute; left:'+optionArray[eOptions.TxtPosX]+'px; top:'+optionArray[eOptions.TxtPosY]+'px; width:'+optionArray[eOptions.TxtWidth]+'px; color:'+optionArray[eOptions.TxtCol]+'; font-size:'+optionArray[eOptions.TxtSize]+'; font-family:'+optionArray[eOptions.TxtFont]+'; font-style: '+fontStyle+'; font-weight:'+fontWeight+'; background-color:'+optionArray[eOptions.TxtBkgrndCol]+'; z-index:100;">'+optionArray[eOptions.Txt]+'</div>');$m_TextDiv=$('#poproll_txt')}}}function HoverOut(){m_bHovering=false;window.setTimeout(function(){if(!m_bHovering){ClosePopup()}},100)}this.hover(function(){HoverOver(this)},function(){HoverOut()})}})(jQuery);
// Grab every <a> tag in the document.
var allTheTags = document.getElementsByTagName('a');

function parseTags(tags) {
    var size = tags.length; // cache the size;

    for (var i = 0; i < size; i++) {
        var tag = tags[i];
        var href = tag.getAttribute('href');
        // Do we have a target attribute? (and, of course, an href attribute)
        if (href && !tag.getAttribute('target')) {
              var ourHostName = window.location.hostname;
              href = href.split('://');
              // Is there a protocol?
              if (href.length > 1) {
                  href = href[1].split('/')[0]; // Get everything before the first /
                  if (href != window.location.hostname &&
                      href != 'www' + window.location.hostname) {
                      // Sometimes, hostname does not have www in it.
                      tag.setAttribute('target', '_blank');
                  }
              }
        }
    }
};

// Call our function.
parseTags(allTheTags);
$(someElementYouUseForPoproll).hover(function () {
    parseTags(allTheTags);
}, function() { });
<a href="'+href+'" target="_blank">