What is href=[removed];

前端 未结 8 1035
天命终不由人
天命终不由人 2020-12-31 18:42

in a code which i am goin through there is a link has href=javascript:; in code.when it is clicked it opens a lightbox to show some msg with close button.how is it done.I th

8条回答
  •  天命终不由人
    2020-12-31 19:08

    Supposedly, it's a URL to a resource that's reachable via the "javascript" protocol, just like you can have "http:" or "ftp:". I don't know if it's an actual standard but most browsers understand that the URL must be fed to the JavaScript interpreter. So, in practise, you can use it to have JavaScript code that's triggered by a link, e.g.:

    Say hello
    

    Of course, writing your JavaScript code inside HTML tags is neither clean nor mantainable. But the possibility exists.

    What about href="javascript:;"? If you pay close attention, you'll realise that ";" is a JavaScript code snippet that, well, does nothing. That's the point. This is often used to have a link that points nowhere. The main purpose is that clicking on it triggers JavaScript code defined somewhere else (via onclick event handlers).

    Last but not least, you often see stuff like onclick="javascript:doStuff()". The onclick HTML attribute expects Javascript code, not a URL. In this situation, the javascript: prefix is totally superfluous. However, the code still runs. It happens to be a label in JavaScript syntax just by chance ;-)

提交回复
热议问题