Click to See Complete Forum and Search --> : Calling JavaScript function from <a href>
BananaQuaalude
10-13-2003, 10:27 PM
Why will this not work? When the page loads, I get an "Expected Identifier" error message. When the link is clicked, I get "'Microsoft JScript runtime error: Object Expected' was not handled" and it highlights "redirect(this)":
<a href="[url]" id="emp" onClick="redirect(this)">View Multiple Employees</a>
The function looks like this:
<script language="javascript">
function redirect(this)
alert(this.id)
</script>
Basically what I want to do here is, on the onClick event of this link, change the action attribute of a form and submit it.
magus
10-13-2003, 11:09 PM
try this:
<a href="javascript:redirect(this)" id="emp" >View Multiple Employees</a>
and adding some code to [url] on the script:
<script language="javascript">
function redirect(this)
alert(this.id) ;
location=[url]
</script>
hope this work
Charles
10-14-2003, 04:59 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
function redirect (link) {
alert (link.id);
location = link.href;
return false;
}
// -->
</script>
<a href="http://www.w3.org" id="emp" onclick="return redirect(this)">Test</a>[/font]
1) Do not use the "javascript" pseudo scheme in the "href" attribute value. It will give you some unwanted results on moddern browsers.
2) You need to make sure that the page still works when there is no JavaScript.
3) You need to actually define the function.
4) The word "this" is reserved and has a special meaning.
BananaQuaalude
10-14-2003, 02:01 PM
Thanks to both of you-
Charles, I was able to get your method to work after putting {} around my function.
I just love switching back and forth between languages!
thanks again,
BQ