2

code3.js:8 Uncaught TypeError: Cannot read property 'charAt' of undefined at espaces_debut (code3.js:8) at onload (Ex4.htm:11)

In Ex4.htm :

<html>

<head>
<title>Ex4</title>
<script language="javascript" src="code3.js">
ch=prompt ("ch ") ; 
alert (espaces_debut(ch));
</script>
</head>
<body onload="espaces_debut()">
</body>
</html>

IN code3.js :

function espaces_debut(ch)
{
i=-1;
do 
{
i=i+1
}   
while (ch.charAt(i)=' ')  ;
return ( ch.substr(0,i-1) ) ;   
}

I get this Error , help pls

yuriy636
  • 11,171
  • 5
  • 37
  • 42
Saief Zaneti
  • 33
  • 1
  • 4

1 Answers1

0

You try to call function escapes_debut on onload without parameter. ch is undefined and has no charAt method.

Remove onload attribute from body.

EDIT: due to comment below:
You don't need this function, where are more errors. Use just .trim() function.

ch.prompt('ch');
alert(ch.trim());

Note that prompt has two optional parameters. The first one isn't value, but label. Value is the second one, if is set.

pavel
  • 26,538
  • 10
  • 45
  • 61
  • I did that before , i don't get an error but the page is not asking me to write (ch ) while i'm writing ch=prompt ("ch ") ; , sorry , i'm just a beginner x) – Saief Zaneti Nov 04 '17 at 17:16
  • @SaiefZaneti: what's your goal? What should your code has to do? – pavel Nov 04 '17 at 17:30
  • My Code is supposed to read a string (in the html file ) , delete the spaces at the beginning and return it (in the function in the js file ) . then show the result in the html file .I can add the function to the main html file and it will work fine , but i'm supposed to do them in separate files . – Saief Zaneti Nov 04 '17 at 17:34
  • @SaiefZaneti: see my edited answer... you don't need your function. – pavel Nov 04 '17 at 17:38