简体   繁体   中英

Register Java Script only if proper event occured


I want to fire script on page only when user triggers proper event. In my case user by button click adds new record to database. I'am able to obtain dynamically created control reference with new record in code behind , I just don't know how from code behind pass it this single time to JavaScript that will fire after page loads.
My script will look like this :

function(id) {
   document.getElementByID(id).focus()
}

I'am using .net framework 1.1
Do you have idea how I can accomplish this task ?

You need to consider

  1. What is the EventTarget ? If you want to wait until the whole page has finished loading, then window is the EventTarget you're looking for because it's the top-most DOM related Object you can reference.
  2. Which event do you want to listen for? Again, if you want to detect page loading, you're listening for load , which is the name of the event.

So, using EventTarget.addEventListener

window.addEventListener('load', function () {
    // code to execute when this handler is invoked
});

Edit: I'm not sure your environment will have window , if it doesn't then you'll probably want the highest Node you can access in your DOM structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM