开发者

Javascript onunload

I have most of this script written but I know I am missing one key pice.

I need to call function (see below)...

function InternalLink() {
Page_ShowPopOnExit = false;
}

...in an if/else statement somehow inside (see code below) to detect that what has been clicked was an internal link and not a page close/external link.

    function LinkConvert() {
var href;
                var anchors = document.getElementsByTagName('a');

                for(var y=0; y<anchors.length; y++)
                {
                                href = anchors[y].href.toLowerCase();
                                if (!(href.indexOf("http://")!=-1 && h开发者_如何学JAVAref.indexOf(MySiteDomain)==-1))
                                                {
                                                anchors[y].clickhandler=InternalLink()
                                                XBrowserAddHandlerPops(anchors[y],"click","clickhandler");
                                                }
                }
}

The (latest) total code is below, Ty for helping me finish my code (or fix it :)

function exitwindow() {
var Page_ShowPopOnExit=true;
var MySiteDomain='www.example.com';
var url = '/exit.html';
var win = 'toolbar=0,directories=0,menubar=0,scrollbars=0,resizable=0,width=250,height=300';
if (Page_ShowPopOnExit==true){
   open(url,'WindowName',win);
   } else (Page_ShowPopOnExit==false)
   {alert('Internal Link')}
}

function InternalLink() {
  Page_ShowPopOnExit = false;
}

function LinkConvert() {
var href;
                var anchors = document.getElementsByTagName('a');

                for(var y=0; y<anchors.length; y++)
                {
                                href = anchors[y].href.toLowerCase();
                                if (!(href.indexOf("http://")!=-1 && href.indexOf(MySiteDomain)==-1))
                                                {
                                                anchors[y].clickhandler=InternalLink()
                                                XBrowserAddHandlerPops(anchors[y],"click","clickhandler");
                                                }
                }
}
function XBrowserAddHandlerPops(target,eventName,handlerName) {
  if ( target.addEventListener ) {
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) {
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else {
    var originalHandler = target["on" + eventName];
    if ( originalHandler ) {
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);};
    } else {
      target["on" + eventName] = target[handlerName];
    }
  }
}


Guessing from your code you want to make a popup when a visitor clicks a link that links to different domain.

Your code currently does this by attaching an onclick handler to all links if the link URL looks like an internal link (there is no "http://" or the domain is present). It will then set a boolean Page_ShowPopOnExit to false which should prevent the popup hook from generating a popup when it is called (exitwindow - which isn't being called in your sample code)

I think there is a simpler way. Namely just attach an onclick handler that will make a popup to each link that isn't an internal link:

function createPopup() {
    var url = '/exitexample.html';
    var win ='toolbar=0,directories=0,menubar=0,scrollbars=0,resizable=0,width=250,height=300';
    open(url,'WindowName',win);
}

var anchors = document.getElementsByTagName('a');
for(var y=0; y<anchors.length; y++) {
    href = anchors[y].href.toLowerCase();
    if (href.indexOf("http://") !== -1 && href.indexOf(MySiteDomain) !==-1) {
        anchors[y].onclick = createPopup; // you might want to replace this with attachEventListener etc.
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜