admin管理员组文章数量:1406924
I want to set a cookie value when user clicks on a link. I am using the following code:
<script src=".7.2.min.js"></script>
<script src="../../Scripts/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
function changeLang(lang) {
$.cookie('myCulture', lang);
window.location.reload();
return false;
}
});
</script>
HTML
<a href="#" onclick="changeLang('da-DK')"><img src="../../Content/images/danishFlag.png" height="35px" width="35px"/></a>
<a href="#" onclick="changeLang('sv-SE')"><img src="../../Content/images/swedishFlag.png" height="35px" width="35px"/></a>
It looks like very simple code, but when I click on the link, there is an error in the browser. It says.
ReferenceError: changeLang is not defined
changeLang("da-DK");
Where am I doing wrong??
I want to set a cookie value when user clicks on a link. I am using the following code:
<script src="http://code.jquery./jquery-1.7.2.min.js"></script>
<script src="../../Scripts/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
function changeLang(lang) {
$.cookie('myCulture', lang);
window.location.reload();
return false;
}
});
</script>
HTML
<a href="#" onclick="changeLang('da-DK')"><img src="../../Content/images/danishFlag.png" height="35px" width="35px"/></a>
<a href="#" onclick="changeLang('sv-SE')"><img src="../../Content/images/swedishFlag.png" height="35px" width="35px"/></a>
It looks like very simple code, but when I click on the link, there is an error in the browser. It says.
ReferenceError: changeLang is not defined
changeLang("da-DK");
Where am I doing wrong??
Share Improve this question asked Dec 6, 2012 at 10:29 Reza.HoqueReza.Hoque 2,75010 gold badges52 silver badges80 bronze badges 1- relocate your function to out of document ready – AliRıza Adıyahşi Commented Dec 6, 2012 at 10:31
5 Answers
Reset to default 2you are defining the function inside the document ready scope, so it's not global therefore not available in the global scope
define it as a global simply by removing the var declaration or using window.changeLang = function
$(document).ready(function () {
changeLang = function(lang) {
document.cookie = 'myCulture' + lang;
window.location.reload();
return false;
}
});
define function outside of the jquery block
$(document).ready(function () {
});
function changeLang(lang) {
$.cookie('myCulture', lang);
window.location.reload();
return false;
}
Try moving the ChangeLang function to outside of the Document Ready section. Also, if running in FireFox with Firebug you can experiment with executing the function directly from the Console.
Good luck
Please do not attach js functions in html like this. You're using jquery so just do this:
$(document).ready(function () {
function changeLang() {
$.cookie('myCulture', $(this).data('lang'));
window.location.reload();
return false;
}
$('.link').on('click', changeLang);
});
and then in html:
<a href="#" data-lang="da-DK">...</a>
$(document).ready(function () {
});
function changeLang(lang) {
document.cookie = 'myCulture=' + lang;
window.location.reload();
return false;
}
This will work for you.
本文标签: jquerycall javascript function on link clickStack Overflow
版权声明:本文标题:jquery - call javascript function on link click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744976092a2635524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论