如何在 ContentPlaceHolder 中运行 JavaScript?
当所有内容都在一个页面中时,我有一堆 JavaScript 运行良好。现在我已经将其分离出来,我不确定如何包含 javascript,在 master 中插入 HEAD 标签,而且,我有在主体加载时运行的函数,但它们也在 master 中。
顺便说一句,这是一个 .NET 2.0 应用程序。
I had a load of javascript that ran well when everything was in one page. Now that I've separated it out, I'm not sure how to include the javascript, ssing the HEAD tag in in the master, and also, I had functions that ran when the body loaded but they are in the master too.
By the way this is a .NET 2.0 app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有特定于某些页面的脚本,并且使用一个主
MasterPage
,只需在母版页的头部添加一个ContentPlaceHolder
即可。母版页:
内容页:
提供给浏览器的最终结果(通常):
现在,您添加到内容页的任何脚本都将位于基本脚本之后。使您可以灵活地使用特定于页面的脚本。
请注意:VS2005 将抛出警告/错误,指出 head 标记中不允许使用
ContentPlaceHolder
。忽略它,这是VS的一个bug。 VS2008+不会为此打扰你。If you have scripts that are specific to certain pages, and you use one main
MasterPage
, simply add aContentPlaceHolder
in the head of the master page.Masterpage:
content page:
End result served to browser (in general):
Now any scripts you add to your content page will be after your base scripts. Gives you the flexibility to have page-specific scripts.
And note: VS2005 will throw up a warning/error that
ContentPlaceHolder
isn't allowed within the head tag. Ignore it, it's a bug in VS. VS2008+ will not bother you about it.一般来说,您可以将多个页面使用的常见 JavaScript 放入一个单独的文件中,并从母版页链接到该文件,并且可以通过使用将任何一次性功能包含在其他任何地方
Generally, you would put common javascripts that more than one page use into a separate file and link to it from the master page and any one off functions can be included anywhere else by using
母版页的概念是针对服务器端的,您的浏览器将只看到一页结果,因此您可以将 JavaScript 函数放在母版的头部部分,如果需要,可以在内容页面中调用它们,或者在母版的其余部分中调用它们页。
编辑
您可以在母版页的头部部分放置一个内容占位符,因此如果您愿意,子页面可以在头部包含其特定脚本...
示例
子示例
The concept of masterpage is for serverside, your browser will see the result as only one page so you can put your javascript functions on the head section of your master, and call them in your content pages if you want or in the rest of the master page.
EDIT
You can put a content place holder on your head section in your master page, so child page can include its specific script in the head if you want...
Example
A child example