Okay so i am using the following code.
<html>
<head>
<link type="text/css" rel="stylesheet" href="html.css" />
<script type="text/javascript">
function getName()
{
if(name)
alert("you thought i forgot you" + name + "?");
else
name=prompt("what's your name","here");
}
</script>
</head>
<body onload="var name;">
<p onclick="getName()"; >click here</p>
</body>
</html>
Now if on clicking the <p> text first time it prompts me, and if I click cancel without entering my name, and then again click the <p> text, it doesnt prompt me anything,
instead it shows the name to be filled as NULL. now in C,
char a=NULL;
if(a)
evaluates to false, doesn’t it happen in javascript?
One thing you might be running into: when you set the global
name, you are actually settingwindow.name.window.nameis a special property that actually persists even after refreshing the page.Try making a page with the script:
On the first run, it should alert “undefined”. If you refresh the page, though, you should see “hello”.
You can avoid the problem by renaming
nameto something else.