admin管理员组文章数量:1323335
I want to have both href and onclick working and I found a solution here: HTML tag <a> want to add both href and onclick working
In myFunction, I make an AJAX Request calling a perl ponent.
It works fine when I put the Javascript call in href, but not when in onclick. Unfortunately, I'm not allowed this solution neither jQuery, so can you please help.
// AJAX Request doesn't work
<a href="www.mysite" onclick="myFunction();">Item</a>
<script type="text/javascript">
function myFunction () {
var xmlHttp = getXMLHttpObject();
xmlHttp.open('GET', 'perl.m', true);
return true;
}
</script>
// AJAX Request ok
<a href="javascript: myFunction('www.mysite')">Item</a>
<script type="text/javascript">
function myFunction (url) {
var xmlHttp = getXMLHttpObject();
xmlHttp.open('GET', 'perl.m', true);
window.location.href = goto_url;
}
</script>
perl.m:
<%init>
warn "perl.m";
... update ...
</%init>
I want to have both href and onclick working and I found a solution here: HTML tag <a> want to add both href and onclick working
In myFunction, I make an AJAX Request calling a perl ponent.
It works fine when I put the Javascript call in href, but not when in onclick. Unfortunately, I'm not allowed this solution neither jQuery, so can you please help.
// AJAX Request doesn't work
<a href="www.mysite." onclick="myFunction();">Item</a>
<script type="text/javascript">
function myFunction () {
var xmlHttp = getXMLHttpObject();
xmlHttp.open('GET', 'perl.m', true);
return true;
}
</script>
// AJAX Request ok
<a href="javascript: myFunction('www.mysite.')">Item</a>
<script type="text/javascript">
function myFunction (url) {
var xmlHttp = getXMLHttpObject();
xmlHttp.open('GET', 'perl.m', true);
window.location.href = goto_url;
}
</script>
perl.m:
<%init>
warn "perl.m";
... update ...
</%init>
Share
Improve this question
edited May 23, 2017 at 11:51
CommunityBot
11 silver badge
asked Aug 8, 2014 at 6:53
Karin SuelKarin Suel
1032 gold badges3 silver badges9 bronze badges
2 Answers
Reset to default 2Please carefully follow the tutorial. It states that you must enter the javascript like this (with return keyword).
<a href="www.mysite." onclick="return theFunction();">Item</a>
Besides that you should always type in a URL (like http://stackoverflow.).
Please let me know if this supports you.
Try with:
<a href="www.mysite." onclick="myFunction(event);">Item</a>
<script type="text/javascript">
function myFunction (e) {
e.preventDefault();
var xmlHttp = getXMLHttpObject();
xmlHttp.open('GET', 'perl.m', true);
return true;
}
</script>
Because you have href
attr with url, so it will go to url and your ajax is not affected.
e.preventDefault()
is your solution here.
本文标签: javascriptHTML Tag ltagt with both href and onclick and AJAX RequestStack Overflow
版权声明:本文标题:javascript - HTML Tag <a> with both href and onclick and AJAX Request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742137335a2422427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论