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 ()" >CLICK</A> do it honestly, don't be.
Hyperlink Tag Binding JS Event && do not add "javascript:;" The resulting cup