JavaScript Code to check if a script is present in the website or not


JS Code to check the script is present in a website or not

Introduction

Many times it happens that you are unable to find the script tag or you are unable to check that if a script tag is present or not. In this article, we are going to how we can check a by using a simple js (JavaScript) code.

Code


var scripts = document.getElementsByTagName("script");
/*
Till Here we have all the scripts in our scripts variable
*/

for (i in scripts){
    if (scripts[i].src == "addyoursrcvaluehere" ){
    
    //you can change the attribute from src to anything and also check that

      console.log("The Link is present");
      console.log(scripts[i]);
    }
}

Demo of How to Use the Code


1. Go to the Website

where you wanna check the code (Here: www.chainblockworld.com)

JS Code to check the script is present in a website or not
Go to Website you wanna check the script

2. Press Ctrl + Shift + I to open Inspect window or right click inspect

JS Code to check the script is present in a website or not
Click on inspect to go to inspect window

3. Go to Console in top navbar

JS Code to check the script is present in a website or not
Click on > in the console

4. Paste the Code with link changed to the link you wanna check


In my case the script is this, so I am going to use the src of the given link

JS Code to check the script is present in a website or not
Here I can checking this script

as you can see I have pasted the code with the link changed to link I want to check

paste the code in the console where a > arrow is showing

JS Code to check the script is present in a website or not
Paste the code in the console window

5. The Output

JS Code to check the script is present in a website or not
output is displayed showing that the script is present

The link is present output has come. If we don't get this output, this means that the link is not present.

Code VERSION 2

Sometimes we get undefined in output so in that case you can use this code. This will display an alert message on the browser screen.

var scripts = document.getElementsByTagName("script");
/*
Till Here we have all the scripts in our scripts variable
*/

for (i in scripts){
    if (scripts[i].src == "addyoursrcvaluehere" ){
    //you can change the attribute from src to anything and also check that

        alert("The Link is present");
    }
}

Output:

result displayed. Blockchain world one of the best blogs on blockchain niche
result displayed

Conclusion

We were able to check a script presence in the website. You can ask your doubts if you have any queries using the comment section.

30 views0 comments

Recent Posts

See All