[英]onclick event on anchor tag running partial function
Belwo is my function which i want to execute on click event of anchor tag. Belwo是我要在锚标记的click事件上执行的函数。 I have tried two methods but none of them working completely fine.
我尝试了两种方法,但没有一种可以完全正常工作。
1st way: 第一种方式:
<a href="/" id="btn_shopping" onClick="logContinueShopping();">Continue Shopping</a>
In this way my function is not working unless i put event.PreventDefault() within my function. 这样,除非将event.PreventDefault()放入函数中,否则函数将无法正常工作。 But as i put event.PreventDefault() in my logContinueShopping() function, the function works fine but anchor tag is not working as obvious.
但是,当我在logContinueShopping()函数中放入event.PreventDefault()时,该函数工作正常,但锚标记作用不明显。
2nd way: 第二种方式:
<a href="#" id="btn_shopping" onClick="logContinueShopping(); document.location='/'">Continue Shopping</a>
In this way, my function is running till ajax request but the success function is not executing ie it is not setting the cookie and redirection the page to '/'. 这样,我的函数一直运行到ajax请求,但是成功函数未执行,即未设置cookie并将页面重定向到“ /”。
function logContinueShopping(){
var action = 'continue_shopping';
// Check if the activity is already logged
var checkCookie = getCookie('continue_shopping');
if(checkCookie == ''){
$.ajax({
type: 'POST',
url: '/ajaxCall/user-activity-ajax.php',
data: {
action:action,
url:url
},
success: function(response)
{
document.cookie = "continue_shopping = " + action + "; path=/";
}
});
}
I would request to please not to provide jQuery solution as im forced to use javascript here. 我要求不要提供jQuery解决方案,因为我被迫在此处使用javascript。
Thanks alot. 非常感谢。 }
}
Have you thought about setting the document.location
in your success
handler? 您是否考虑过在
success
处理程序中设置document.location
? : :
$.ajax({
type: 'POST',
url: '/ajaxCall/user-activity-ajax.php',
data: {
action: action,
url: url
},
success: function (response) {
document.cookie = "continue_shopping = " + action + "; path=/";
document.location = '/';
}
});
Try this: change href="javascript:void(0)" 试试这个:change href =“ javascript:void(0)”
and user window.location="/" on success of the ajax call to redirect. 以及ajax调用成功重定向用户window.location =“ /”。
I have placed async: false
and used the function in below way 我已经放置了
async: false
并以下面的方式使用了该函数
<a href="javascript:void(0)" id="btn_shopping" onClick="logContinueShopping(); window.location='/'">Continue Shopping</a>
Thanks for contribution. 感谢您的贡献。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.