简体   繁体   中英

Max length for a function parameter

Is there any max length for a JavaScript function() parameter?

I created a function which had a long parameter, but it didn't work. When I shortened the parameter of the same function, it worked. Does that mean that there is a maximum length for a function argument? If so, please recommend alternatives to my current method.

JavaScript

function example(idnum) {
   alert(idnum);
}

HTML

<div onclick='example(*php variable,no special character included*)'></div>

When the PHP variable is long, such as "17b6f557969619544eb1e6cd58c3f341", it does not work. But if I change the variable to something like "203", the function works successfully.

"Douglas Crockford" wrote in message news:9a027$3fa7c56d$44a4aebc$9152@msgid.meganewsse rvers.com... [color=blue]

... . The maximum length with will be implementation-specific. ...[/color]

In microsoft.public.scripting.jscript, Michael Harris (Microsoft.MVP.Scripting), who might be expected to know, quoted:-

In JScript, variant string variables have the same limit as in VBScript, up to 2^31 characters.

String literals (as in "this is a literal") have (IIRC) a limit ~2^10 (1024) characters.

  • for the JScript implementation.

Blockquote

Sounds like you actually want to pass the PHP variable's value literally to the function, not as a variable name.

example(17b6f557969619544eb1e6cd58c3f341)

tries to call the function with that variable (likely causing an exception, or even being a syntax error), while

example(203)

calls the function with the number literal for the integer 203 . If you want to pass the value as a string, use json_encode !

Also notice that you will need to escape everything for use in a HTML attribute (ie escape quotes, < and >), as you have

<div onclick=" [some code here] ">

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM