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.
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.
Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String?
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.prototyp… More on forum.freecodecamp.org
How to find a substring position in a string ? something like indexOf in js
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. More on elixirforum.com
Do you know? The indexOf() function can take a second parameter.
I never had to use for loop in javascript More on reddit.com
Find the Index of the First Occurrence in a String
The loop is for the starting character index to check. And an indexed string expression: str[i] will only return one character. Not the string which starts from the given character index. Use the string's slice() method instead. Giving it the starting character index, and the exact same length as the check string. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice More on reddit.com
Videos
02:53
What is the .indexOf() String Method? | JavaScript in LESS-THAN ...
09:00
indexOf and lastIndexOf methods | String Object In JavaScript - ...
07:13
Chapter 4: substring() and indexOf() methods - YouTube
00:56
indexOf() in Javascript 🔥 #javascript #DSA #javascriptinterview ...
05:03
Javascript Basics · String · indexOf() (method) - YouTube
02:02
JavaScript Basics - indexOf() - YouTube
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 )
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.
freeCodeCamp
forum.freecodecamp.org › javascript
Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String? - JavaScript - 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-string-indexof-method
JavaScript String indexOf() Method - GeeksforGeeks
Here’s a simple example that finds the position of a substring within a string: ... // Original string let str = 'Departed Train'; // Finding index of occurrence of 'Train' let index = str.indexOf('Train'); console.log(index);
Published January 16, 2026
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
DigitalOcean
digitalocean.com › community › tutorials › how-to-index-split-and-manipulate-strings-in-javascript
How To Index, Split, and Manipulate Strings in JavaScript | DigitalOcean
August 24, 2021 - To summarize, charAt() and slice() will help return string values based on index numbers, and indexOf() and lastIndexOf() will do the opposite, returning index numbers based on the provided string characters.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › indexOf
Array.prototype.indexOf() - JavaScript | MDN
The indexOf() method compares searchElement to elements of the array using strict equality (the same algorithm used by the === operator).
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.indexof()
JavaScript String indexOf() Method
November 3, 2024 - Summary: in this tutorial, you’ll learn how to use the JavaScript String indexOf() method to find the index of the first occurrence of a substring within a string.
DEU University
web.deu.edu.tr › doc › oreily › web › jscript › refp_363.html
[Chapter 21] Reference: String.indexOf()
The search begins at position start within string, or at the beginning of string, if start is not specified. If an occurrence of substring is found, then String.indexOf() returns the position of the first character of the first occurrence of substring within string.
30 Seconds of Code
30secondsofcode.org › home › javascript › string › all indexes of substring
Find all indexes of a substring in a JavaScript string - 30 seconds of code
March 10, 2024 - const indexOfSubstrings = function* (str, searchValue) { let i = 0; while (true) { const r = str.indexOf(searchValue, i); if (r !== -1) { yield r; i = r + 1; } else return; } }; [...indexOfSubstrings('tiktok tok tok tik tok tik', 'tik')]; // [0, 15, 23] [...indexOfSubstrings('tutut tut tut', 'tut')]; // [0, 2, 6, 10] [...indexOfSubstrings('hello', 'hi')]; // [] ... JavaScript generator functions are an advanced yet very powerful ES6 feature, which you can start using in your code right now. ... Get all the partial substrings of a string in JavaScript using generator functions.
DigitalOcean
digitalocean.com › community › tutorials › js-indexof
Exploring the indexOf Method for Strings and Arrays in JavaScript | DigitalOcean
December 17, 2019 - An overview of how the indexOf() method works to find the position of a substring in a string or an item in an array in Javascript.