Javascript not opening class specific links with out HTML href -
Javascript not opening class specific links with out HTML href -
i running problem want links class = "okpop" open, links local html files called popupa.html , popupb.html. in order stop popup default open links using href made href undefined. there links nothing. want js code trigger popup window nil happening.
html code:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>popup windows</title> <!--[if lt ie 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <!-- script 9.4 - popups.html --> <!-- bullet 8: add together class --> <p><a href="javascript:undefined" class="okpop" id="b" target="popup">b link</a> (will open in new window.)</p> <p><a href="javascript:undefined" class="1" id="a" target="popup">a link</a> (will open in new window.)</p> <script src="js/popups.js"></script> </body> </html>
javascript code
// function called when link clicked. function createpopup(e) { 'use strict'; // event object: if (typeof e == 'undefined') var e = window.event; // event target: var popupclass = e.class || e.srcelement; var link = "popup" + document.getelementbyid(id) + ".html"; // create window: var popup = window.open(link, 'popup', 'height=100,width=100,top=100,left=100,location=no,resizable=yes,scrollbars=yes'); // give window focus if it's open: if ( (popup !== null) && !popup.closed) { popup.focus(); homecoming false; // prevent default behavior. } else { // allow default behavior. homecoming true; } } // end of createpopup() function. // found functionality on window load: window.onload = function() { 'use strict'; // add together click handler each link: (var = 0, count = document.links.length; < count; i++) { // if statement trigger specific class value : bullet 9 if (document.links[i].classname == "okpop"){ if (document.getelementsbyclassname('okpop').value){ popuplinks[i].onclick = createpopup; }else{ document.links[i].onclick = createpopup; } // end of loop. }; // end of onload function.
to begin with, script has number of syntax errors; you're missing 2 closing braces in window.onload function. should this:
// found functionality on window load: window.onload = function() { 'use strict'; // add together click handler each link: (var = 0, count = document.links.length; < count; i++) { // if statement trigger specific class value : bullet 9 if (document.links[i].classname == "okpop"){ if (document.getelementsbyclassname('okpop').value){ popuplinks[i].onclick = createpopup; }else{ document.links[i].onclick = createpopup; } // close 'else' case } // close 'if doc.links[i].classname' case } // end of loop. }; // end of onload function.
upon fixing that, you'll reference error createpopup - id undefined. also, when constructing link, you're asking entire element when want element's id:
// function called when link clicked. function createpopup(e) { 'use strict'; // event object: if (typeof e == 'undefined') { // utilize braces clarity. also, redefining // var e within if statement might not behave way expect. e = window.event; } // event target: var popupclass = e.class || e.srcelement; // event target never reference it. //we have element, utilize id build link: var link = "popup" + popupclass.id + ".html"; // create window: var popup = window.open(link, 'popup', 'height=100,width=100,top=100,left=100,location=no,resizable=yes,scrollbars=yes'); // give window focus if it's open: if ( (popup !== null) && !popup.closed) { popup.focus(); homecoming false; // prevent default behavior. } else { // allow default behavior. homecoming true; } } // end of createpopup() function.
try - it's working on end, tested on chrome.
javascript href eventlistener
Comments
Post a Comment