1

I'm using this javascript to replace all of my links with registration messages:

<script>
function replaceLinks() {
    var links = document.querySelectorAll('.restore a:link');
    for (var i = 0; i < links.length; i++) {
        links[i].innerHTML = 'DOWNLOAD register here.';
        links[i].href = 'register.php';
    }
}
</script>

The problem:

It's replacing all thumbnail images (that link to their full-size images), too. I can't apply a specific CSS selector to just links because I'm using vBulletin and it would apply it to both links and images inside of the post content.

Does anyone know how I can...

forbid this Javascript from applying to links that end with .jpg, .gif, etc -- so it only rewrites the links, and not the thumbnails that link to their full sized images?

3
  • If it helps any, this is what the images use for their link/css <a id="attachment234234234" rel="Lightbox_5234234" href="http://www.domain.com/attachments/filename123123123.png"> <img class="thumbnail" border="0" pagespeed_url_hash="2832013083" style="float:CONFIG" alt="Blah blah blah" src="http://www.domain.com/attachments/filename123123123.png" title="blah blah blah"></img> Commented Apr 10, 2014 at 5:32
  • @AmitJoki developer.mozilla.org/en-US/docs/Web/CSS/:link Commented Apr 10, 2014 at 5:41
  • check out my answer too :) Commented Apr 10, 2014 at 5:49

3 Answers 3

2

You can check link href with simle regexp and proceed only if test is false:

function replaceLinks() {
    var links = document.querySelectorAll('.restore a:link');
    for (var i = 0; i < links.length; i++) {
        if (/\.(png|jpe?g)$/.test(links[i].href)) continue;
        links[i].innerHTML = 'DOWNLOAD register here.';
        links[i].href = 'register.php';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am seriously, madly in love with you right now. THANK YOU! Yes, it worked perfectly. You are a genius.
2

Change this line var links = document.querySelectorAll('.restore a:link'); to:

var links = document.querySelectorAll('.restore a:link:not([href$=".jpg"]):not([href$=".jpeg"]):not([href$=".png"])');

Comments

0

That's just too easy to avoid (just seeing the code, or deactivating javascript), I suggest (if you can) install a plugin or make it you (maybe just a patch).

In fact, I think you can disable anonymous download permission in the built-in settings, without installing anything. But if you wanna do it with a JavaScript, at least add some style (then deactivating JavaScript will not show the link):

Try this code, first without JavaScript, then run replaceLinks()

<style>
.restore a:link:not([href$=".jpg"]):not([href$=".jpeg"]):not([href$=".png"]){display:none}
</style>
<script>
function replaceLinks() {
    var links = document.querySelectorAll('.restore a:link:not([href$=".jpg"]):not([href$=".jpeg"]):not([href$=".png"])');
    for (var i = 0; i < links.length; i++) {
        console.log(i,links[i]);
        links[i].innerHTML = 'DOWNLOAD register here.';
        links[i].href = 'register.php';
        links[i].style.display='inherit';
    }
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.