In the previous example, after the foo() function is done, the user will be sent to
CHAPTER 2 n HTML, CSS, AND JAVASCRIPT 39
mylink.html. To prevent this default action from happening, you need to return false as the
last part of the onclick attribute:
My LinkAlternatively, the function can determine whether true or false should be returned and
passed back to the onclick handler:
My LinkThis is most commonly seen in form handlers, in which any errors in form validation
return false, preventing the form from being submitted to the server. If no errors were found,
it returns true, and the form is submitted to the server.
For links, you can use the javascript: pseudoprotocol:
My LinkI definitely do not recommend that you use this practice because it??™s sloppy and promotes
inaccessible coding practices. What do I mean by inaccessible practices? I mean inaccessible
for search bots (that currently don??™t understand JavaScript) and inaccessible for users who
have JavaScript disabled. It??™s best to always have a default behavior that is overridden by the
event handler.
Here is an example for doing a pop-up window:
My LinkIf users have JavaScript enabled, clicking this link opens it in a new window.
Pages:
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91