1

I am trying to make a script that counts how many external scripts I have on my site.

$( document ).ready(function() {
    $tags = $("script[src*='http']");
    $.each($tags, function(index, value) { console.log("INDEX: " + index + " VALUE: " + value ); });
});

Index is working as intented, but the value that is printed to the console is [object HTMLScriptElement]. How can I modify my script to print the value for src attribute?

I already tried these two changes:

  1. From $("script[src*='http']") to $("script[src*='http']").attr('src')
  2. From value to value.toString()
0

3 Answers 3

2

use $(value).attr("src") or value.src to get src attribute of script tag, change to:

...
$.each($tags, function(index, value) { 
    console.log("INDEX: " + index + " VALUE: " + $(value).attr("src") ); 
    //OR
    console.log("INDEX: " + index + " VALUE: " + value.src ); 
});
...
Sign up to request clarification or add additional context in comments.

Comments

1
$(document).ready(function () {
    $tags = $("script[src*='http']");
    $.each($tags, function (index, value) {
       console.log("INDEX: " + index + " VALUE: " + $(value).attr("src") );
    });
});

Demo: http://jsfiddle.net/Bh6Vz/1/

Comments

0

Try like this

$( document ).ready(function() {
$tags = $("script[src]");
$.each($tags, function(index, value) { console.log("INDEX: " + index + " VALUE: " + value  ); });
});

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.