m3nda
m3nda

Reputation: 2017

Javascript comparison between an array value and a string

Consider this code:

<script type="text/javascript" src="some.js"></script>
<script type="text/javascript" src="some2.js"></script>

<script type="text/javascript">

    var scripts = document.getElementsByTagName("script");

    for (i=0;i<scripts.length;i++){

        if(scripts[i].src=="some.js") {
        scripts[i].src = "somechanged.js";
        }
    }


</script>

<script type="text/javascript" src="some.js"></script>

I dont know why the if is not working. If you comment with // the if it will work for all readed script src. I try some tricks like toLowerCase() and/or valueOf(), but none worked.

I change the if to if(1==1) and of course it works.

What im doing wrong into the string comparison?

Regards

Upvotes: 0

Views: 105

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

Added comment as answer on par with your request.

  • src properties are absolute URL. This is specified in the specification.

  • You want the actual attribute you set it to, rather than the property the DOM is actually using to get it.

  • If you want the attribute in the HTML, you can use scripts[i].getAttribute("src") which will return the correct value.

Upvotes: 1

Related Questions