Use a debugger to find out what you are getting in the userInput. The code is fine. It should work. See sample code below.
test = function() {
var test = "http://Test 2"
alert(test.substring(0, test.indexOf("://")))
}
Answer from user883986 on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › indexOf
String.prototype.indexOf() - JavaScript | MDN
The indexOf() method of String values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number.
Top answer 1 of 2
1
Use a debugger to find out what you are getting in the userInput. The code is fine. It should work. See sample code below.
test = function() {
var test = "http://Test 2"
alert(test.substring(0, test.indexOf("://")))
}
2 of 2
0
You need to pass the value to the findProtocol method rather than DOM element
Replace
var userInput = document.getElementById("txtURL");
by
var userInput = document.getElementById("txtURL").value;
and replace
if (URL = true)
with
if( URL == true )
Videos
00:56
indexOf() in Javascript 🔥 #javascript #DSA #javascriptinterview ...
07:04
JavaScript Substring Method in 5 Minutes - YouTube
06:32
Master Substring Searches in JavaScript! Quick & Easy Tutorial” ...
07:13
Chapter 4: substring() and indexOf() methods - YouTube
03:55
indexOf Method Javascript | Javascript Tutorial - YouTube
Common String Methods: Retrieving Substrings | JavaScript ...
W3Schools
w3schools.com › jsref › jsref_indexof.asp
JavaScript String indexOf() Method
The indexOf() method returns the position of the first occurrence of a value in a string.
Mimo
mimo.org › glossary › javascript › string-indexof-method
JavaScript String indexOf() method: Syntax, Usage, and Examples
The indexOf() method in JavaScript allows you to find the position of a substring within a string. It returns the index of the first occurrence or -1 if the substring doesn’t exist.
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-indexof-method
JavaScript String indexOf() Method - GeeksforGeeks
The indexOf() method can be used to search for longer substrings: ... // JavaScript to illustrate indexOf() method // Original string let str = 'Departed Train'; // Finding index of occurrence of 'Train' let index = str.indexOf('ed Tr'); ...
Published January 16, 2026
Elixir Forum
elixirforum.com › t › how-to-find-a-substring-position-in-a-string-something-like-indexof-in-js › 68052
How to find a substring position in a string ? something like indexOf in js | Elixir Forum
December 13, 2024 - I have read the docs of String module, it seems that there’s nothing like IndexOf in js which return index of a substring in string. | Elixir Forum
TutorialsPoint
tutorialspoint.com › javascript › pdf › string_indexof.pdf pdf
JavaScript String indexOf() Method
It can be any · integer between 0 and the length of the string. The default value is 0. Return Value · Returns the index of the found occurrence, otherwise -1 if not found. Example · Try the following example. <html> <head> <title>JavaScript String indexOf() Method</title> </head> <body> ...
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-find-the-index-of-specific-character-in-a-string-in-javascript
JavaScript - Index of a Character in String - GeeksforGeeks
July 23, 2025 - It works similarly to indexOf() but searches from the end of the string. JavaScript · let s = "Welcome to GeeksforGeeks"; let c = "G"; let index = s.lastIndexOf(c); console.log(index); Output · 19 · The search() method is a powerful tool used to search for a specific substring within a string.
Scaler
scaler.com › home › topics › indexof() in javascript
indexOf() in JavaScript- Scaler Topics
July 5, 2022 - The indexOf method in JavaScript is mainly used in string searching operations. The indexOf() method of the String class returns the index of the first occurrence of the substring in a string.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
The index of the first character to include in the returned substring.
freeCodeCamp
forum.freecodecamp.org › curriculum help
Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String? - Curriculum Help - The freeCodeCamp Forum
January 9, 2025 - Tell us what’s happening: Am I missing something, or is there an issue with the following concept check question? The official correct answer is (C), but isn’t (B) correct as well? According to MDN: String.prototype.indexOf() Array.prototype.indexOf() Your browser information: User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0 Challenge Information: Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String?
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-find-the-index-of-the-first-occurrence-of-a-substring-in-a-string
JavaScript Program to find the Index of the First Occurrence of a Substring in a String - GeeksforGeeks
July 23, 2025 - Example: Here is an example to find the index of the first occurrence of a substring in a string using the indexOf() method.
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-find-the-index-of-an-element-that-contains-the-given-substring-in-array
How to Find the Index of an Element that Contains the Given Substring in Array ? - GeeksforGeeks
August 5, 2025 - const array = ['apple', 'banana', ... present at index", index); ... Use the findIndex() with indexOf() method to check if the substring exists in any of the element contained by the array....
Instagram
instagram.com › reel › DIeAIP8z7S6
Need to find text inside a string? Rafael explains how ...
We cannot provide a description for this page right now
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › lastIndexOf
String.prototype.lastIndexOf() - JavaScript | MDN
The method returns the index of the last occurrence of the specified substring at a position less than or equal to position, which defaults to Infinity. If position is greater than the length of the calling string, the method searches the entire string.
W3Schools
w3schools.com › JAVA › ref_string_indexof.asp
Java String indexOf() Method
charAt() codePointAt() codePointBefore() codePointCount() compareTo() compareToIgnoreCase() concat() contains() contentEquals() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() isEmpty() join() lastIndexOf() length() matches() offsetByCodePoints() regionMatches() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toCharArray() toLowerCase() toString() toUpperCase() trim() valueOf() Java Math Methods ·
Web Reference
webreference.com › javascript › basics › string-methods
JavaScript String Methods Explained
The .indexOf() and .lastIndexOf() methods search for a specified string within another string, and return the index of the first occurrence.