1

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.

Marvin
  • 853
  • 2
  • 14
  • 38
shadyinside
  • 630
  • 1
  • 8
  • 23
  • 6
    Let's see the code you were using. – David G Dec 15 '12 at 17:02
  • What exactly does "it didn't work" mean? Did you get an error? – Pointy Dec 15 '12 at 17:04
  • 1
    Maybe you have unsupported characters in your function argument? Please read variables restrictions at mdn https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals – antyrat Dec 15 '12 at 17:04
  • actually the parameter is a php variable. I have done similar php variable insertions in function parameter but, they were short, so i guess worked. But the same method doesn't work with lengthy php variables. – shadyinside Dec 15 '12 at 17:05
  • 2
    We need some code and examples. – PitaJ Dec 15 '12 at 17:07
  • 1
    Can you share the output of `
    = $your_var ?>
    ` (from page source) so that we know what's happening there?
    – inhan Dec 15 '12 at 17:19
  • There is no limitation. but it depends on implementation – defau1t Dec 15 '12 at 18:26
  • possible duplicate of [Maximum length of variable name in JavaScript](http://stackoverflow.com/questions/1809153/maximum-length-of-variable-name-in-javascript) – Bergi Dec 15 '12 at 19:30
  • Did you define the variable `17b6f557969619544eb1e6cd58c3f341` anywhere, or did you output a string literal or something? – Bergi Dec 15 '12 at 19:34

2 Answers2

-1

"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

Mike
  • 3,348
  • 11
  • 45
  • 80
  • Where did you copy that quote (including the bb markup) from, or is it just a formatting mistake and "Douglas Crockford wrote…" is not supposed to be part of the quote? – Bergi Dec 15 '12 at 19:32
  • Seems to be from http://bytes.com/topic/javascript/answers/92088-max-allowed-length-javascript-string#post308604 – Gumbo Dec 15 '12 at 19:49
-1

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 (i.e. escape quotes, < and >), as you have

<div onclick=" [some code here] ">
Bergi
  • 630,263
  • 148
  • 957
  • 1,375