tags:

views:

48

answers:

3

I need to verify a lot of links on a page. Rather than opening each link myself. This is what I did.

jquerified the page using firequery plugin. Then I typed following code in firebug.

a = $('a');
$.each(a, function(i,val){
  $val = $(val);
  $val.attr({target: '_blank'});
  $val.trigger('click');
});

Even though I am trigger click the links were not clicked. Why?

A: 

trigger('click') just doesn't work. I had the same problem recently and resolved it by using click().

Nissan Fan
click() is also not working
Nadal
I will pull the code later today. If it wasn't click() then it was OnClick(); or some minor variant. I'll post soon.
Nissan Fan
+1  A: 

You can do like this, ok you'll have problems with popup blockers but if this is only for debugging purposes you can simply disable blocker and that's it.

a = $('a');
$.each(a, function(i,val){
  window.open(val, '_blank');
});

Here is the whole code and it worked for me. Actually I didn't test it on a server, just checked html file on my desktop. Firefox doesn't allow popups to display even I said to display popups but IE has option to allow popups for local files and it works, opens two windows for google and yahoo.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="jquery-min.js"></script>
    </head>
    <body>
        <a href="http://www.google.com"&gt;aa&lt;/a&gt;
        <a href="http://www.yahoo.com"&gt;bb&lt;/a&gt;
        <script>
            $(document).ready(function() {
                    a = $('a');
                    $.each(a, function(i,val){
                    window.open(val, '_blank');  
                });
            });
        </script>
    </body>
</html>
Ergec
not sure why but this solution is not working.
Nadal
@dorelal, I updated my answer.
Ergec
should be `window.open(val.attr('href'), '_blank')`
Felix
@Felix, val has href value by default from function.
Ergec
@Ergec, wrong. `val` is the current HTML element. I was wrong, too, that line should be `window.open(val.href, '_blank')`, since val is not a jQuery instance. **Note:** $.each cycles through everything in the `a` array, which is an array of HTML elements.
Felix
@Ergec, I think Firefox didn't work for a reason. I also think IE is doing its usual weirdness accepting `HTMLAnchorElement` s for `window.open()`.
Felix
@Felix: If I do alert(val) and alert(val.href) both popups the same result, the URL.
Ergec
That's because alert calls `.toString()` on the parameter before displaying the alert box. `window.open()` shouldn't do that. And it doesn't, on compliant browsers such as Firefox. On IE, on the other hand..
Felix
A: 

This code works for me:

$("a").each(function(i, val) { window.open(val.href); });

However, Chrome blocks this code because it tries to open some 20 popups at once, but I can see that it does indeed try to open them.

Felix