Menu

Docker vulnerabilities and fixes

 Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

How to get a segment from a URL using JS?

We can find a text or get a segment from a URL or web address using the JS split and slice function. Below is an example of a slice and split function in simple JavaScript code.

Lets assume we have a URL: https://rashidjorvee.blogspot.com/content/2015/javascript:getName_function/12032/live.html

When we run the split based on colon(:) it will return as below.

e.g. window.location.href.split(":").pop()

result ==> getName_function/12032/live.html

Now, from this if you want to read only few characters from any index, then do the slice on the URL.

e.g. window.location.href.split(":").pop().slice(0,7);

result ==> getName

How to read any attribute value using the element class name using JS?

Using the DOM function we could find the element using class name and then from that search list go to first index and get the required attribute.

document.getElementsByClassName('class_name')[0].getAttribute('id');