
When you aren’t explicit in your statements, it’s more likely you’ll lead to complications or erratic behavior on other platforms.

In general, the more that is explicitly stated, the better. This is especially true for markup languages (HTML, CSS) and web-based languages (JavaScript) which don’t have a compiler, but an interpreter. In programming, there’s a lot that’s assumed. It also tells the A tag that onClick it should run the JavaScript function. It tells the link that the “HREF” tag is completely null and that it should not do anything. The above is a better example of programming. So, you’re actually using JavaScript in two places, now! You’re using it in the HREF, but also in the onClick function. Instead, you should explicitly state, within the HREF, that the HREF reference (the page to navigate to) doesn’t exist: When you have a blank “#”, it just assumes you want to go to the top of the page, because it can’t find any other explicit references. For instance, “#tos” might link to your “Terms of Service” header, or “#social” might link to your social media links.
CALL JAVASCRIPT FUNCTION ON RESIZE CODE
If your code is only a single page long, “#” isn’t going to do anything at all.īut “#” is actually used to designate positions within a page. Many people don’t realize this because they’re testing their code on a very small page. Your HREF shouldn’t be “#” because that causes navigation to the top of the page. In fact, if you look up HREF JavaScript right now, that’s probably going to be one of the most common examples you see.īut this actually isn’t ideal either. Most people will tell you that you should use the following code instead: It gets the message across, but it does so in a way that’s not generally documented or supported. In other words, it’s sort of like it’s against the “grammar” of the programming language. While it works, this makes it “messy” and unintuitive. So, if you use your HREF attribute in this way, you’re using it in a way that goes against the original intent of the attribute. HREF is meant to act as an identifier - a reference - rather than an action. But it’s extremely deprecated for an important reason. It will be, essentially, identical to using the onClick function of the link. On a procedural and computer level, it won’t cause any issues. Unfortunately, though it’s a pretty easy technique, it’s also considered deprecated. It’s an easy way to setup a user-controlled interface - users are used to clicking on links and the developer doesn’t have to scan for any specific action from the user. But you could also use the onHover action to process the JavaScript when the user just puts their cursor on the link, or a variety of other “on mouse move” actions.ĭevelopers often use “A” links in this way because it’s just very easy. When you call a JavaScript function through the HREF, it’s similar to onClick JavaScript - the script function processes when you click the link. In fact, you can create JavaScript code that launches within the HREF attribute of a URL: Click And then the closes the tag.īut the HREF attribute doesn’t have to include a URL. The HREF attribute is where the link goes. In the above, the tag is the beginning of the link. You know it as any link that generally shows up as blue and underlined: a connection to another page.Ī hyperlink, in HTML, is written as follows: Google Web Search A hyperlink is the basic foundation of the internet. In this case, though, we aren’t using buttons. Buttons are usually used in HTML forms, but other than form submission, they need JavaScript for more advanced functionality.

In fact, an interesting element is that this button would essentially be a worthless element without using JavaScript to process some form of code. The JavaScript_Function() itself would be at the top of the page (“HEAD” section), but the button would be in the middle of the page (“BODY” section). The JavaScript that you wrote would be processed when the user “clicked” the button. But though the “HEAD” section stores the code, it’s usually called in the “BODY.” Calling JavaScript code means that you need to know “when” that code is going to be processed - usually based on a user action. Anything within the HTML tag is essentially the code of the website page. The “HEAD” area of an HTML page can store JavaScript code. Before we understand why we’d use the HREF attribute, we should understand how we use JavaScript in a website. Most people are familiar with HTML before they start programming in JavaScript, but not all. The most up-to-date JS resource online! Master Javascript by building a beautiful portfolio of projects! | By Colt Steele, Stephen Grider Explore Course Calling a JavaScript Function through HREF
