[英]Javascript function only works once
我的网页上有一个内联脚本,例如:
<script type="text/javascript">
var rootPath = '<%= Url.Content("~/") %>';
$(document).ready(function () {
$('#logonStatus').click(function () { loadLoginForm(); });
alert('Document Ready');
});
function loadLoginForm() {
if(!serenity.tools.isStyleSheetLoaded('redmond.css')) {
$('head').append('<%= Url.StyleTag("Redmond/redmond.css", MediaTypes.Screen) %>');
}
if(!serenity.tools.elementExists($('#logonContainer'))) {
$.ajax({
async: false,
cache: false,
datatype: 'html',
success: function (data) { $('body').append(data); },
type: 'GET',
url: '/Membership/LogOn'
});
}
$('#logonContainer').dialog({
modal: true,
hide: 'slide'
});
}
</script>
我还正在加载一个自定义javascript文件,其内容如下:
var serenity = new function () {
$(document).ready(function () {
jQuery.ajaxSetup({
beforeSend: function (xhr) {
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
}
});
});
this.tools = new function () {
this.isStyleSheetLoaded = function (fileName) {
$(document.styleSheets).each(function () {
if (this.href.toLowerCase().indexOf(fileName) != -1) {
this.isStyleSheetLoaded = true;
return;
}
});
this.isStyleSheetLoaded = false;
}
this.elementExists = function (element) {
this.elementExists = element.length != 0;
}
}
}
由ajax调用加载的文件只是一个带有包含输入元素的表的div。 该文件中不含任何JavaScript。
我的问题是,第一次调用isStyleSheetLoaded可以正常工作,但是在加载文件并显示对话框并关闭对话框之后,我单击了触发loadLoginForm函数的链接,但是这次它说isStyleSheetLoaded不是函数。 这在所有浏览器中都显示出来,因此我99%确信这是我的问题,但我不知道这是什么。 有人能指出我正确的方向吗?
提前致谢。
我认为您的问题如下:
您定义了一个函数“ this.isStyleSheetLoaded = function(fileName)”,但是在他的体内,您重写了此属性“ this.isStyleSheetLoaded = true;”。
因此,在您第一次调用isStyleSheetLoaded之后,该函数将被布尔值覆盖。
正确的方法可能是:
this.isStyleSheetLoaded = function (fileName) {
$(document.styleSheets).each(function () {
if (this.href.toLowerCase().indexOf(fileName) != -1) {
return true;
}
});
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.