Hướng dẫn set href javascript

An element is invalid HTML unless it has either an href or name attribute.

Nội dung chính

  • Definition and Usage
  • Browser Support
  • Attribute Values
  • More Examples
  • Definition and Usage
  • Browser Support
  • Property Values
  • Technical Details
  • More Examples
  • Related Pages

If you want it to render correctly as a link (ie underlined, hand pointer, etc), then it will only do so if it has a href attribute.

Code like this is therefore sometimes used as a way of making a link, but without having to provide an actual URL in the href attribute. The developer obviously wanted the link itself not to do anything, and this was the easiest way he knew.

He probably has some javascript event code elsewhere which is triggered when the link is clicked, and that will be what he wants to actually happen, but he wants it to look like a normal tag link.

Some developers use href='#' for the same purpose, but this causes the browser to jump to the top of the page, which may not be wanted. And he couldn't simply leave the href blank, because href='' is a link back to the current page (ie it causes a page refresh).

There are ways around these things. Using an empty bit of Javascript code in the href is one of them, and although it isn't the best solution, it does work.

❮ HTML tag

Example

The href attribute specifies the link's destination:

Visit W3Schools

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The href attribute specifies the URL of the page the link goes to.

If the href attribute is not present, the tag will not be a hyperlink.

Tip: You can use href="#top" or href="#" to link to the top of the current page!


Browser Support

Attribute
href Yes Yes Yes Yes Yes

Syntax

Attribute Values

ValueDescription
URL The URL of the link.

Possible values:

  • An absolute URL - points to another web site (like href="http://www.example.com/default.htm")
  • A relative URL - points to a file within a web site (like href="default.htm")
  • Link to an element with a specified id within the page (like href="#section2")
  • Other protocols (like https://, ftp://, mailto:, file:, etc..)
  • A script (like href="javascript:alert('Hello');")


More Examples

Example

How to use an image as a link:


Hướng dẫn set href javascript

Try it Yourself »

Example

How to link to an email address:

Send email

Try it Yourself »

Example

How to link to a phone number:

+47 333 78 901

Try it Yourself »

Example

How to link to another section on the same page:

Go to Section 2

Try it Yourself »

Example

How to link to a JavaScript:

Execute JavaScript

Try it Yourself »


❮ HTML tag

❮ Anchor Object

Example

Change the destination (URL) of a link:

document.getElementById("myAnchor").href = "http://www.cnn.com/";

Try it Yourself »


Definition and Usage

The href property sets or returns the value of the href attribute of a link.

The href attribute specifies the destination of a link.


Browser Support

Property
href Yes Yes Yes Yes Yes

Syntax

Return the href property:

Set the href property:

Property Values

ValueDescription
URL Specifies the URL of the link.

Possible values:

  • An absolute URL - points to another web site (like href="http://www.example.com/default.htm")
  • A relative URL - points to a file within a web site (like href="default.htm")
  • An anchor URL - points to an anchor within a page (like href="#top")


Technical Details

Return Value:A String, representing the URL of the link. Returns the entire URL, including the protocol (like http://)

More Examples

Example

Get the URL of a link:

var x = document.getElementById("myAnchor").href;

Try it Yourself »


Example

Another example of how to get the URL of a link (a relative URL):

var x = document.getElementById("myAnchor").href;

Try it Yourself »


HTML reference: HTML href attribute


❮ Anchor Object