2

i wants to delete all css file from my page on page load after topHeader div closing except one which inside top header for that i have written some jquery code but its not working for please anyone can help?

<div class="topHeader" style="background-color: #0a5fa2 !important">
    <div class="stripe">
        <div class="row">
            <div class="col-md-4">
                <p class="needHelp">Need Help?</p>
                <p class="helpDesk">Hotel Reservation Helpdesk (Working Hours TO 8PM)</p>
            </div>
            <div class="col-md-8">
                <div class="wprTopicon">
                    <span class="whyBookWith">Why book with us ?</span>
                    <div class="secureTrans">SECURE
                        <br> TRANSACTIONS</div>
                    <div class="bestRateGurtee">BEST PRICE
                        <br>GUARANTEED</div>
                    <div class="bestOffer">BEST OFFER</div>
                </div>
            </div>
        </div>
    </div>
    <link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
</div>//topHeader closed
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates- common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<script>
$(function() {
    var count = 1;
    $('link').each(function() {
        var href = $(this).attr('href');
        if (href == "http://wotfly.com/templates-common/106/css/newheader.css") {
            if (count > 1) {
                $(this).remove();
            }
            count++;
        }
    });
});
</script>

Remove function is not deleting the css file  from the page please help?
4
  • put all the stylesheet in <head></head> tag Commented Jun 4, 2015 at 5:30
  • all css dynamically genrated we can't pull inside head Commented Jun 4, 2015 at 5:38
  • try $("link[href*='http://wotfly.com/templates-common/106/css/newheader.css']").remove(); Commented Jun 4, 2015 at 5:45
  • this will delete all css file with same href while i want to delete all except one Commented Jun 4, 2015 at 5:57

3 Answers 3

2

You can simply use :not(:first) to exclude first link and remove rest

$("link[href*='http://wotfly.com/templates-common/106/css/newheader.css']:not(:first)").rem‌​ove();
Sign up to request clarification or add additional context in comments.

Comments

0

There is two problems :

  • You misspelled function ready.
  • extra space there on link no 2. href="http://wotfly.com/templates- common/

See below snippet. And inspect the element to see the link exist or not.

$(function () {
    var count = 1;
    $('link').each(function (i, e) {
        
        var href = $(this).attr('href');
        if (href == "http://wotfly.com/templates-common/106/css/newheader.css") {
            
            if (count > 1) {
                $(this).remove();
            }
            
        }
        count++;
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="topHeader" style="background-color: #0a5fa2 !important">
    <div class="stripe">
        <div class="row">
            <div class="col-md-4">
                <p class="needHelp">Need Help?</p>
                <p class="helpDesk">Hotel Reservation Helpdesk (Working Hours TO 8PM)</p>
            </div>
            <div class="col-md-8">
                <div class="wprTopicon"> <span class="whyBookWith">Why book with us ?</span>

                    <div class="secureTrans">SECURE
                        <br>TRANSACTIONS</div>
                    <div class="bestRateGurtee">BEST PRICE
                        <br>GUARANTEED</div>
                    <div class="bestOffer">BEST OFFER</div>
                </div>
            </div>
        </div>
    </div>
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
</div>
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">

4 Comments

Hi thanks for the rply but that's not an issue...this code is not able to delete css file.
and one more thing, try put count++; outside if statement. see updated code
You can not delete any file on server with javascript or jquery
if we put count++; outside page also containing so many link and each loop run for all links and count will increase and it will delete all css file with given href while i want to delete all except one
0

It should work...

var elements = document.querySelectorAll("link");

for(var i=0; i < elements.length; i++) {
    var el = elements[i];
    if(el.parentNode.className !== "topHeader") {
        $(el).remove()
    }

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="topHeader" style="background-color: #0a5fa2 !important">
    <div class="stripe">
        <div class="row">
            <div class="col-md-4">
                <p class="needHelp">Need Help?</p>
                <p class="helpDesk">Hotel Reservation Helpdesk (Working Hours TO 8PM)</p>
            </div>
            <div class="col-md-8">
                <div class="wprTopicon"> <span class="whyBookWith">Why book with us ?</span>

                    <div class="secureTrans">SECURE
                        <br>TRANSACTIONS</div>
                    <div class="bestRateGurtee">BEST PRICE
                        <br>GUARANTEED</div>
                    <div class="bestOffer">BEST OFFER</div>
                </div>
            </div>
        </div>
    </div>
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
</div>
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">
<link rel="stylesheet" type="text/css" href="http://wotfly.com/templates-common/106/css/newheader.css">

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.