有三种方式。
首先是把脚本标签放在页面底部:
<body>
<!--Body content-->
<script type="text/javascript">
alert('<%: TempData["Resultat"]%>');
</script>
</body>
第二种方法是创建一个 onload 事件:
<head>
<script type="text/javascript">
window.onload = function(){//window.addEventListener('load',function(){...}); (for Netscape) and window.attachEvent('onload',function(){...}); (for IE and Opera) also work
alert('<%: TempData["Resultat"]%>');
}
</script>
</head>
它将在窗口加载时执行一个函数。
最后,第三种方式是创建一个readystatechange
事件并检查当前的document.readystate:
<head>
<script type="text/javascript">
document.onreadystatechange = function(){//window.addEventListener('readystatechange',function(){...}); (for Netscape) and window.attachEvent('onreadystatechange',function(){...}); (for IE and Opera) also work
if(document.readyState=='loaded' || document.readyState=='complete')
alert('<%: TempData["Resultat"]%>');
}
</script>
</head>