Discussions » Development

Help opening specific link on page but not others

§
Posted: 2016-09-18
Edited: 2016-09-18

Help opening specific link on page but not others

I am trying to open specific links on the webpage. There is exactly 100 that should be opened yet when I run this script:

this.$ = this.jQuery = jQuery.noConflict(true); //make a safe instance of jQuery

var baseUrl = "details.php?id=";

var matchingLinks = $( "a[href*='" + baseUrl + "']" );

$(matchingLinks).each( 
    function(index)
    {
        window.open( $(this).attr( "href" ) );
    }
);

I end up with 405 pages being opened. What I need is to block out these links:

userdetails.php?id= smalldetails.php?id= details.php?id=21361&hit=1&filelist=1 (with this one the number varies so I bring that to your attention)

wOxxOmMod
§
Posted: 2016-09-18

Show an example of a link that should be opened.

§
Posted: 2016-09-18

I don't use jQuery, but in JavaScript, maybe this would resolve the problem with the first two patterns:

var matchingLinks = document.querySelectorAll('a[href^="' + baseUrl + '"],'a[href*="/' + baseUrl + '"]');

In your loop, you can check for and skip over the third pattern

§
Posted: 2016-09-18

Show an example of a link that should be opened.

http://pastebin.com/mE29t280

I don't use jQuery, but in JavaScript, maybe this would resolve the problem with the first two patterns:

var matchingLinks = document.querySelectorAll('a[href^="' + baseUrl + '"],'a[href*="/' + baseUrl + '"]');

In your loop, you can check for and skip over the third pattern

You have helped many times in the past Jeff but I have yet to actually sit down and learn to code on my own. I am just pretty good at modifying what others set out for my own needs. So I am lost when you say it that way.

wOxxOmMod
§
Posted: 2016-09-18
Edited: 2016-09-18

Well, since the href attribute starts with the required string, try using ^ instead of *:

var matchingLinks = $( "a[href^='" + baseUrl + "']" );

BTW in modern browsers you can use ES6 template strings via backticks:

var matchingLinks = $( `a[href^="${baseUrl}"]` );

Post reply

Sign in to post a reply.