Suppose I have two links:
<a href="#" onclick="myFunction();"></a>
<a href="#" onclick="myFunction(1, 2, 'string');"></a>
My goal is to parse the myFunction
call and get an array of the parameters. So from the first link I would want to get []
and from the second link I would want to get [1, 2, 'string']
.
I already know how to get the onclick
attribute into a string (we are using jQuery here), so that doesn't need to be part of the answer.
Basically:
var myCall="myFunction(1, 2, 'string')";
parseFunctionString(myCall); //[1, 2, 'string']
Define parseFunctionString
. I am imagining this would msot easily be done with some sort of regex.
You can do this literally with the arguments
keyword, within a function. For example:
function foo() {
alert(arguments[0]);
}
foo('bar');
that will alert 'bar'
, since that's the first argument to the function. You can use the arguments
keyword just like an array, but it won't have built-in methods like arrays do, so it won't have things like .sort()
. More info can be found on MDN .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.