What is link function in javascript?

❮ JavaScript String Reference

Example

let text = "Free Web Building Tutorials!";
let result = text.link["//www.w3schools.com"];

Try it Yourself »

Definition and Usage

String link[] is deprecated in JavaScript.

Avoid using it.

It may cease to work in your browser at any time.

The link[] method returns a string embedded in an string

Syntax

Parameters

Parameter Description
url Required.
The URL to link to.

Return Value

A string embedded in an

or


Edit:

The above response is really not a good solution, having learned a lot about JS since I initially posted. See EndangeredMassa's answer below for the better approach to solving this problem.

answered Mar 27, 2009 at 1:38

6

Unobtrusive JavaScript, no library dependency:



    

        // Wait for the page to load first
        window.onload = function[] {

          //Get a reference to the link on the page
          // with an id of "mylink"
          var a = document.getElementById["mylink"];

          //Set code to run when the link is clicked
          // by assigning a function to " title"
          a. title = function[] {

            // Your code here...

            //If you don't want the link to actually 
            // redirect the browser to another page,
            // "google.com" in our example here, then
            // return false at the end of this block.
            // Note that this also prevents event bubbling,
            // which is probably what we want here, but won't 
            // always be the case.
            return false;
          }
        }
    


    linky        


answered Mar 27, 2009 at 1:50

EndangeredMassaEndangeredMassa

16.9k8 gold badges53 silver badges79 bronze badges

12

Clicky

EDIT, years later: NO! Don't ever do this! I was young and stupid!

Edit, again: A couple people have asked why you shouldn't do this. There's a couple reasons:

  1. Presentation: HTML should focus on presentation. Putting JS in an HREF means that your HTML is now, kinda, dealing with business logic.

  2. Security: Javascript in your HTML like that violates Content Security Policy [CSP]. Content Security Policy [CSP] is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting [XSS] and data injection attacks. These attacks are used for everything from data theft to site defacement or distribution of malware. Read more here.

  3. Accessibility: Anchor tags are for linking to other documents/pages/resources. If your link doesn't go anywhere, it should be a button. This makes it a lot easier for screen readers, braille terminals, etc, to determine what's going on, and give visually impaired users useful information.

answered Mar 27, 2009 at 1:35

Matt GrandeMatt Grande

11.7k6 gold badges60 silver badges87 bronze badges

5

And, why not unobtrusive with jQuery:

  $[function[] {
    $["#unobtrusive"].click[function[e] {
      e.preventDefault[]; // if desired...
      // other methods to call...
    }];
  }];

HTML

jquery

answered Mar 27, 2009 at 1:54

Mister LuckyMister Lucky

4,0332 gold badges19 silver badges18 bronze badges

3

Unobtrusive Javascript has many many advantages, here are the steps it takes and why it's good to use.

  1. the link loads as normal:

    click here

this is important becuase it will work for browsers with javascript not enabled, or if there is an error in the javascript code that doesn't work.

  1. javascript runs on page load:

     window.onload = function[]{
            document.getElementById["DaLink"]. title = function[]{
                   if[funcitonToCall[]]{
                       // most important step in this whole process
                       return false;
                   }
            }
     }
    
  2. if the javascript runs successfully, maybe loading the content in the current page with javascript, the return false cancels the link firing. in other words putting return false has the effect of disabling the link if the javascript ran successfully. While allowing it to run if the javascript does not, making a nice backup so your content gets displayed either way, for search engines and if your code breaks, or is viewed on an non-javascript system.

best book on the subject is "Dom Scription" by Jeremy Keith

answered Mar 28, 2009 at 15:27

Fire CrowFire Crow

7,2594 gold badges35 silver badges35 bronze badges

I think you can use the title event, something like this:


answered Mar 27, 2009 at 1:36

David ZDavid Z

124k26 gold badges249 silver badges275 bronze badges

Or, if you're using PrototypeJS

Chủ Đề