views:

143

answers:

4

I'm trying to sort through the links on my page and make some of them open into a new window, depending on their URL. This is the code I have. It doesn't seem to be working. Can you see why?

function MakeMenuLinksOpenInNewWindow() {
    var links = document.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        if (links[i].href == "http://testtesttest.org/")
            links[i].target = "_blank";
    }
}
MakeMenuLinksOpenInNewWindow();
+3  A: 

Use jQuery.js. It'll make your life much easier:

$("a[href='http://testtesttest.org/']").attr("target", "_blank");
James Curran
his javascript is fine. telling him to replace it with the equivalent jquery won't fix his problem.
lincolnk
+2  A: 

Make sure that when you call this function the DOM has been loaded:

window.onload = MakeMenuLinksOpenInNewWindow;

or:

<body onload="MakeMenuLinksOpenInNewWindow();">
Darin Dimitrov
Is there a way to call the function from the .js file?
Christian
The reason your function did not work is because the DOM (and therefore the a links) had not yet loaded so your script had nothing to do. What Darin's code does is bind your function to the execution of the onload event of the document (in other words, once the DOM is loaded and ready, your function executes). If you add the first code snippet he suggested to your .js file, everything should work.
Moses
Yes, try my first suggestion: `window.onload = MakeMenuLinksOpenInNewWindow;`.
Darin Dimitrov
But other than that, the function is good?This is what I have in the .js file now:function MakeMenuLinksOpenInNewWindow() { var links = document.getElementsByTagName("a"); for (var i = 0; i < links.length; i++) { if (links[i].href == "http://byupas.org/") links[i].setAttribute('target', '_blank'); } window.onload = MakeMenuLinksOpenInNewWindow();}
Christian
You could just load .js file as the last element on the page isntead of doing an onload.
David Murdoch
Seriously, we're still recommending `onload`...?
J-P
@J-P - lol. For someone wanting to find all links on the page and change thier target attribute with JS, yes.
David Murdoch
@christian: it should be `window.onload = MakeMenuLinksOpenInNewWindow;`, not `window.onload = MakeMenuLinksOpenInNewWindow();`.
David Murdoch
@David, why?? What part of that requires 'onload'? Why not just place the JS at the bottom of the doc...?
J-P
Using Firebug, I can see that the onload handler is picking up the function, but the DOM inspector says "__onloadStarted" and "onLoadCompleteStarted" are false, long after the page is loaded.
Christian
@J-P. I was first to suggest placing the file at the button of the doc. I just don't think the OP WANTS to do that.
David Murdoch
A: 

You should probably not be setting this javascript. And instead use HTML.

But if you must...

function MakeMenuLinksOpenInNewWindow() {
    var links = document.getElementsByTagName("a");
    for (var i = 0, l = links.length; i < l; i++) {
        if (links[i].href === "http://www.example.com/")
            links[i].target = "_blank";
    }
}
window.onload = MakeMenuLinksOpenInNewWindow;
David Murdoch
I put this into my .js file, and it doesn't seem to be working. Is there anything else I need to do?
Christian
show us the HTML for the page you are talking about.
David Murdoch
also, you may be overriding the window.onload event.
David Murdoch
function MakeMenuLinksOpenInNewWindow() { var linkies = document.links; for (var i = 0; i < linkies.length; i++) { if (linkies[i].href == "http://byupas.org/") linkies[i].target = "_blank"; } alert("HEY SUCKA");}window.onload = MakeMenuLinksOpenInNewWindow;The function is definitely not being called, since the Alert is not displaying.
Christian
Using Firebug, I can see that the onload handler is picking up the function, but the DOM inspector says "__onloadStarted" and "onLoadCompleteStarted" are false, long after the page is loaded.
Christian
Show us some HTML.
David Murdoch
A: 

your javascript looks fine. assuming you're having problems with your link elements not existing before you try to modify them, you need to delay running your method as mentioned in other posts. my preferred method is moving the script contents to the end of the page.

since it looks like you're using an external js file, your page would like

    ... 
    <a href="http://testtesttest.org/" >whatever</a>
    ... 
    <script src="myscriptfile.js"></script>
</body>
</html>

if you're still having problems, you'll have to post a more complete example.

edits: another point. if you're still having problems, make sure the url you are expecting in the href property isn't being rewritten. for example, IE will tack a trailing / to the end of a .com url if you don't provide it, which would cause your comparison to fail.

lincolnk