Hyperlink Tag Binding JS Event && do not add "javascript:;" The resulting cup

Source: Internet
Author: User

for a long time, when writing HTML and JS, we often give hyperlinks <a> tags, and bind JS events.


  I've always had a question, why not add "javascript:;"  . Originally, I am very "honest", every time will add. Today, another hand knocking similar code, the Spirit of "Lao Tzu is not add, you bite me" of the attitude, I did not add "javascript:;"  .

 <a href= "" onclick= "Doremove (${column.id}, ' ${column.name} ')" > Delete </a>

  function Doremove (id,name) {

if (Confirm ("OK to delete:" +name+ "?")) {

$.post (

"/column/doremove.json", {

Id:id

},

function (data) {

Console.log (data);
}
}

The result is very cup.
The background can always receive the request, but the front end is not printing data. Is there a return value?

However, I open a new window and send a separate "Doremove.json" request, which has a return value.

The solution is not to be baffled.
Compared with other normal code, using "Control variable method", gradually to "where the code is different", but I found that the JS code of the idea is exactly the same.

What I found in the process of repeated attempts: the front-end Confrim dialog, the backend has the request, and finally the response.
But the back end has been printed, "Render view/column/list.html".
JS execution is finished, how to render a new page?

Finally, suddenly thought, <a href= "#" >click</a> this HYPERLINK "href" No value, when clicked, the open page is actually "current page", for example
The current page is "Http://localhost:8080/column/list", the open page or the current page.

The background print log, the front end of the data, as well as the previous experience of writing HTML, finally remembered that he did not write "javascript:;" is probably the cause of this result.

Finally, after adding, the code is completely normal.
------------------------------------------
I was thinking that when we often submit form forms, if we return false in the event method, the form will not be submitted.
<input type= "Submit" onclick= "onsubmit ()"/>

"JavaScript;;" Play the same role.

---------------------------------------
<a href= "" onclick= "Return Doremove (${column.id}, ' ${column.name} ')" > Delete </a>
Doremove returns false directly, and the hyperlink is still open.

Final conclusion: <a href= "javascript:;" onclick= "DoAction ()" &GT;CLICK&LT;/A&GT; do it honestly, don't be.

Hyperlink Tag Binding JS Event && do not add "javascript:;" The resulting cup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.