您好我在.aspx页面上测试了以下javascript代码: 我需要将其部署为在子布局.ascx上运行。现在,我知道我可以在标签之间添加我的javascript,但是当我在.aspx页面上运行它时,我也根据一定的加载条件运行Javascript,使用“onload”属性和SetTimeOut ......我想我可以让JS在他的.ascx页面上运行,但是如何让onload在.ascx中工作:这是代码:
<script type="text/javascript">
function showModal() {
<%
RoleItem roleItem = MeauUserSecurity.GetSupportCenterUserRole(Sitecore.Context.User);
if (roleItem.ID == RoleItem.GetEmployeesRole().ID || roleItem.ID == RoleItem.GetSupportCenterAdministratorRole().ID)
{
%>
var url = document.URL;
// var popUp = '<%=Url %>/components/supportcenter/feedback.aspx?value=';
var popUp = 'http://local.meau.com/components/supportcenter/feedback.aspx?value=';
var site = popUp + url;
var runpopUp = 50;
if (runpopUp >= Math.random() * 100 ) {
$(document).ready(function () {
$.fancybox({
'width': 500,
'height': '55%',
'autoScale': false,
'transitionIn': 'none',
'transitionOut': 'none',
'type': 'iframe',
'href': site,
'showCloseButton': false,
'title': 'We Request your Feedback'
});
});
}
<%
}
%>
}
// If the user tries to exit, run showModal
// window.onbeforeunload = showModal();
</script>
答案 0 :(得分:0)
我认为你在这里遵循一个非常糟糕的模式...你最好分割你的JS和你的内容。以下示例使用jQuery进行事件...
<%-- MyAscxName.ascx
Set a class for the ascx, and add in a property from the ascx's code behind definition
--%>
<div class="MyAscxName" data-someprop="<%:SomePropertyValue%>" >
..other controls...
<asp:Button ... CssClass="ActionButton" />
</div>
在您的母版页中,公开应用程序的基本路径。
<script>var basePath = '<%=Url.Content("~/")%>';</script>
然后,您可以将相关的JS包含在您的网站JS或该网站的该部分......
//MyAscxName.js
(function($){
$(document).ready(initialize);
function initialize() {
var instances = $(".MyAscxName");
//check for instances of your control...
if (instances.length == 0) return; //no instances of said control
//delegate events for internal items
instances.delegate('.ActionButton','click',handleActionButtonClick);
instances.each(function(){
var instance = $(this); //rewrap single instance in jquery object
if (instance.attr("data-someprop") == "go") {
//do something here
}
});
}
function handleActionButtonClick(e) {
//example url construction
var url = basePath
+ 'components/supportcenter/feedback.aspx?value='
+ encodeURIComponent(location.toString())
;
//do something...
}
}(jQuery))
通过遵循上述约定,您可以在全局或该部分中包含脚本,并依赖ascx的标记来进行事件绑定。