2

A javascript with "IF" statement written in all pages header template and which is unnecessary only for a specific page, is it possible to disable that statement for a specific page. Below is the javascript which make external links to open in new tab, can i disable this small script only for a google custom search page because the result links have google(external) url which redirects to website(internal), that's why script is reading links as external. Or is there some better way than disabling the if statement? If anyone knows how to solve this problem, please help

Javascript:

$(document).ready(function() {
  $('a').each(function() {
    var a = new RegExp('/' + window.location.host + '/');
       if (!a.test(this.href)) {
            $(this).attr("target", "_blank");
       }
   });
});
3
  • Change the code to omit that specific page? Commented Aug 22, 2015 at 12:00
  • 1
    if (location.href.indexOf('pagename.html') !== -1) ? Commented Aug 22, 2015 at 12:01
  • or if you can't change the header template but can change the indivisual links. You can also change the href value of the link DOM to "javascript:window.open('youlinks.com/blabla', '_self')" Commented Aug 22, 2015 at 13:05

2 Answers 2

1

One way to do it for multiple pages, like this:

var excludedPages = ['blockpage1.html', 'blockpage2.html'];
for (var i = 0; i < excludedPages.length; i++) {
    if (location.href.indexOf(excludedPages[i]) !== -1) {
        // do something if page found
        console.log("this page is blocked for extra code");
    } else {
        // do something if page not found in the list
        console.log("this page is not included in block list");
    }
}

EDIT
Note: The only thing to be aware of with JavaScript, it is running on client side (browser side) and any one with basic web development knowledge are able to change the block site or edit any the site content. This make it possible getting access to what ever site that was blocked. So it all depends how important your blocking mechanism and strategy.

Sign up to request clarification or add additional context in comments.

Comments

0

Probably the easiest way would be to set a flag variable on the custom search page above the script, for example:

var keepInternal = true;

And then modify one line in your script to check for that flag:

$(document).ready(function() {
  $('a').each(function() {
    var a = new RegExp('/' + window.location.host + '/');
       if (!a.test(this.href) && !keepInternal) {
            $(this).attr("target", "_blank");
       }
   });
});

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.