SharePoint:ScriptLink测试
最终结论:
<SharePoint:ScriptLink ID="SPlink1" Name="sp.ui.tgportal.js" Defer="false" runat="server" OnDemand="false" Localizable="true" />
defer: | 决定是否及时输出,默认为“false”,“true”为最后输出; |
OnDemand: |
决定是否由“RegisterSod”进行注册,如果使用注册,则页面加载完后不马上加载js,而是在调用函数前要进行加载,比如使用SP.SOD.executeFunc("sp.ui.tgportal.js", null, alert(0)); |
Localizable: | 决定是否要在2052中查找js,false是直接在layouts中查找; |
LoadAfterUI: | 决定在head中输出还是在body完成前 |
参考:http://www.ilovesharepoint.com/2010/08/sharepoint-scripts-on-demand-spsod.html
2011-12-30添加
SharePoint:ScriptLink在内部调用的也是 ClientScript.RegisterStartupScript RegisterClientScriptBlock RegisterClientScriptInclude
通过js路径来判断是否未重复注册
page2.ClientScript.RegisterStartupScript(typeof(ScriptLink), str, info.ToScriptBlock(page));
str = ExtractFilename(name) + localizable.ToString();
=========================================================
部分测试数据
=========================================================
<SharePoint:ScriptLink ID="SPlink1" Name="sp.ui.tgportal.js" Defer="false" runat="server" />
</head>
结果: header附近
25:document.write('<script type="text/javascript" src="/_layouts/2052/init.js?rev=rcZULplQ2sjYL0GwamiLEQ%3D%3D"></' + 'script>');
26:document.write('<script src="/_layouts/2052/sp.ui.tgportal.js?rev=pLpSQ6vSUCg3DEmdOme1fQ%3D%3D"></' + 'script>');
=========================================================
<SharePoint:ScriptLink ID="SPlink1" Name="sp.ui.tgportal.js" Defer="true" runat="server" />
</head>
结果: RegisterSod之前
<script type="text/javascript">
// <![CDATA[
document.write('<script src="/_layouts/2052/sp.ui.tgportal.js?rev=pLpSQ6vSUCg3DEmdOme1fQ%3D%3D"></' + 'script>');
// ]]>
</script>
<script type="text/javascript">RegisterSod("sp.core.js",…
=================================================================
<SharePoint:ScriptLink ID="SPlink1" Name="sp.ui.tgportal.js" Defer="true" runat="server" OnDemand="true" /></head>
或
<SharePoint:ScriptLink ID="SPlink1" Name="sp.ui.tgportal.js" Defer="false" runat="server" OnDemand="true" />
结果:
<script type="text/javascript">RegisterSod("core.js", "\u002f_layouts\u002f2052\u002fcore.js?rev=KpeTWMrqjCSYYu4RiO0\u00252B1Q\u00253D\u00253D");</script>
<script type="text/javascript">RegisterSod("sp.ui.tgportal.js",
==========================================================
<SharePoint:ScriptLink ID="SPlink1" Name="sp.ui.tgportal.js" Defer="true" runat="server" OnDemand="false" Localizable="true" />
</head>
结果:
<script type="text/javascript">
// <![CDATA[
document.write('<script src="/_layouts/2052/sp.ui.tgportal.js?rev=pLpSQ6vSUCg3DEmdOme1fQ%3D%3D"></' + 'script>');
// ]]>
</script>
<script type="text/javascript">RegisterSod("sp.core.js",
===========================================================
<SharePoint:ScriptLink ID="SPlink1" Name="sp.ui.tgportal.js" Defer="false" runat="server" OnDemand="false" Localizable="true" />
结果:
25:document.write('<script type="text/javascript" src="/_layouts/2052/init.js?rev=rcZULplQ2sjYL0GwamiLEQ%3D%3D"></' + 'script>');
26:document.write('<script src="/_layouts/2052/sp.ui.tgportal.js?rev=pLpSQ6vSUCg3DEmdOme1fQ%3D%3D"></' + 'script>');