In this snippet I'm attempting to extract a substring from the href from a link.
let current_link = $(data, ownerDocument).find('#footbar>span>a[href^="/shortlinks"]').attr('href');
let current_number = current_link.substr(current_link.indexOf('/dex/')+5);
For some reason, when I break on the second and evaluate the line in the console, I get the substring I am expecting, but when I let the script run without a breakpoint, I get the following error:
jQuery.Deferred exception: current_link.substr(...) is not a function
TypeError: current_link.substr(...) is not a function
I added some debugging to prove to myself that current_link
is actually a string:
let current_link = $(data, ownerDocument).find('#footbar>span>a[href^="/shortlinks"]').attr('href');
console.log(typeof(current_link)) // debugging
console.log(Object.getPrototypeOf(current_link)); // debugging
let current_number = current_link.substr(current_link.indexOf('/dex/')+5);
The two console.log lines printed out:
> string
> String {"", constructor: ƒ, anchor: ƒ, big: ƒ, blink: ƒ, …}
> anchor: ƒ anchor()
...
> substr: ƒ substr()
....
> __proto__: Object
> [[PrimitiveValue]]: ""
I have reviewed three questions that are similar, but that I don't think apply, since by the prototype I know that current_link
is a string:
EDIT: I made a jsfiddle that demonstrates the structure of the code where this is occurring. Unfortunately the error is not reproduced in the fiddle, but hopefully showing the structure helps. Fiddle
I can't link to the page where this code is running, because it is part of a TamperMonkey userscript and so is not normally part of the webpage.
SECOND EDIT: This is not actually a problem related to the substring function or string. I needed a semicolon at the end of let current_number = ...
to avoid issues with Automatic Semicolon Insertion (ASI).