How do you set page level, page-SPECIFIC javascript events using a ContentPlaceHolder?
I previously asked how to include JavaScript in my page when I split the page into a MasterPage and ContentPlaceHolder (.NET 2.0 app) The issue was I only wanted the JavaScript functions on THAT page so I couldn't just put them on the masterpage.
Based on the answers, I will include common functions through Maste开发者_Go百科rPage and can put the page-specific function right on the content page. However, 1 question remains: Events. I have 2 JavaScript functions that I wanted to load when the page loads ala the HTML below. How do you load JavaScript page events on the specific content page? Or in the case below, the OnKeyPress event?
<body onkeypress="javascript:keypressed();" onload="javascript:setDivVisibility();">
I would modify my functions keypressed and setDivVisibility to do a check on the location before executing the intended code given your stated constraints.
Something like this:
function keypressed() {
if ( window.location.href !== 'http://www.whatever.com/something/aPage.html' ) {
return;
}
// the original code here
}
It looks like you're using a windows based server, so you might want to do a toLowerCase() on the window.location.href before the comparison as case doesn't matter to windows servers. Good luck!
精彩评论