0

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

9
  • 2
    print the src values. You will see that the urls look different than you think. Commented Nov 14, 2013 at 8:37
  • 2
    src properties are absolute URL. If you want the attribute in the HTML - try scripts[i].getAttribute("src") === "some.js" Commented Nov 14, 2013 at 8:38
  • Also changing the src of the script does not work anyway. stackoverflow.com/questions/10007591/… Commented Nov 14, 2013 at 8:39
  • omg, the src is taking the long path call (localhost/etc/etc) instead the relative url. Commented Nov 14, 2013 at 8:40
  • do a console.log(scripts[i].src) in your script and see what is displayed in the javascript debugging console. Commented Nov 14, 2013 at 8:40

1 Answer 1

1

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.

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

1 Comment

WORKING. thanks! Laziness don't got prizes :) ... make an answer not a comment.

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.