Exclude an external stylesheet appended from a jQuery external file

I’m building a website for a client and at a certain point I had to add a third part jQuery file in order to add some products to my page. The problem is that this very script is adding to my file his own stylesheet in this way:

jQuery.ajaxSetup({
    async: false
});

jQuery('head').append('<link type="text/css" rel="stylesheet" href="http://pathToTheScript/style-products-v2.css">');

I’m wondering if there is a way to exclude this function from the script. I can download locally and I can get rid of the file manually, but the problem is that this third part updates regularly this page, adding a ton a new products.
I’m working in a wordpress environment, so my question is: is there any way to exclude the file with another script so I can replace it with my old stylesheet?

Related posts

Leave a Reply

3 comments

  1. Try this:

    $(document).ready(function(){
        $("link").each(function(){
            if($(this).attr("href")=="pathToTheScript/style-products-v2.css"){
                $(this).remove();
            }
        });
    });
    

    EDIT
    Because this link is inserted by another script…
    The file name of the .css file may change in the future’s third party script updates…

    You should do this by using a regular expression to target the element:

    $(document).ready(function(){
        $("link").each(function(){
            if($(this).attr("href")==/http://domain.com/pathToTheScript/.*.css/){
                $(this).remove();
            }
        });
    });
    

    This will remove link element that has an href value like: http://domain.com/pathToTheScript/ (whatever) .css:

    Example:
    http://domain.com/pathToTheScript/style-products-v2.css
    http://domain.com/pathToTheScript/style-products-v3.css
    http://domain.com/pathToTheScript/improved-style.css
    http://domain.com/pathToTheScript/anything.css

    Side note:
    in the part of the path you keep in the regex (the part you’re sure will not change), you have to escape / and . like this / and this: ..
    You can test your regular expression here: https://regex101.com/#javascript

    Just to be clear
    You have to use the path to the CSS file !
    Not the path to the .js file that injects it.
    😉

  2. Didn’t test, but something like this should work:

    $(document).ready(function() {
        $('link[href="pathToTheScript/style-products-v2.css"]').remove();
    });