Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Modifying onclick event at runtime, problem.

Modifying onclick event at runtime, problem.

Scheduled Pinned Locked Moved Web Development
helpjavascripttoolstutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T Smooth
    wrote on last edited by
    #1

    I have two web pages. The main page contains several VML rectangles that have their "onclick" events set to javascript code. The second page is opened by the first page when a particular link is clicked. What I want to do is change the onclick event of a element on the main page from the 2nd page. So far I am able to retrieve the reference to the object and view it's initial onclick event: // this code is in the 2nd page that was opened by the main page var rectelem = window.opener.document.getElementById('event2_2372'); alert(rectelem.onclick); // this works and shows me what it is initially set to. rectelem.onclick = 'alert(\'This is the new onclick event!\');' ; alert(rectelem.onclick); // shows that the change was made. When I switch back to the main page and click on the object though, the initial onclick behavior is gone but the new behavior does not take place. Nothing happens and I get no script error to look at. I've tried moving the new onclick behavior to a function and then setting the object's onclick to the address of that function: rectelem.onclick = test; function test() { alert('This is the test function!'); } This approach has had the same result so far :( Any ideas on what I'm doing wrong or how to change this events behavior at runtime?

    T 2 Replies Last reply
    0
    • T T Smooth

      I have two web pages. The main page contains several VML rectangles that have their "onclick" events set to javascript code. The second page is opened by the first page when a particular link is clicked. What I want to do is change the onclick event of a element on the main page from the 2nd page. So far I am able to retrieve the reference to the object and view it's initial onclick event: // this code is in the 2nd page that was opened by the main page var rectelem = window.opener.document.getElementById('event2_2372'); alert(rectelem.onclick); // this works and shows me what it is initially set to. rectelem.onclick = 'alert(\'This is the new onclick event!\');' ; alert(rectelem.onclick); // shows that the change was made. When I switch back to the main page and click on the object though, the initial onclick behavior is gone but the new behavior does not take place. Nothing happens and I get no script error to look at. I've tried moving the new onclick behavior to a function and then setting the object's onclick to the address of that function: rectelem.onclick = test; function test() { alert('This is the test function!'); } This approach has had the same result so far :( Any ideas on what I'm doing wrong or how to change this events behavior at runtime?

      T Offline
      T Offline
      T Smooth
      wrote on last edited by
      #2

      I have since been able to get the second approach to work by putting the function test() on the main page and then setting the onclick event = to window.opener.test. This still leaves me with the underlying problem though which is that I need to change what test() does during runtime which I'm not sure how to do.

      G 1 Reply Last reply
      0
      • T T Smooth

        I have since been able to get the second approach to work by putting the function test() on the main page and then setting the onclick event = to window.opener.test. This still leaves me with the underlying problem though which is that I need to change what test() does during runtime which I'm not sure how to do.

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Try to put the function object in the event instead of in the page: rectelem.removed = function() { alert('This is the test function!'); }

        --- b { font-weight: normal; }

        T 1 Reply Last reply
        0
        • G Guffa

          Try to put the function object in the event instead of in the page: rectelem.removed = function() { alert('This is the test function!'); }

          --- b { font-weight: normal; }

          T Offline
          T Offline
          T Smooth
          wrote on last edited by
          #4

          This only works if that element is on the same page as the function. My element and function that I want to be used for the element's onclick event, reside on the main page but I need to modify that function's code from the popup page.

          G 1 Reply Last reply
          0
          • T T Smooth

            I have two web pages. The main page contains several VML rectangles that have their "onclick" events set to javascript code. The second page is opened by the first page when a particular link is clicked. What I want to do is change the onclick event of a element on the main page from the 2nd page. So far I am able to retrieve the reference to the object and view it's initial onclick event: // this code is in the 2nd page that was opened by the main page var rectelem = window.opener.document.getElementById('event2_2372'); alert(rectelem.onclick); // this works and shows me what it is initially set to. rectelem.onclick = 'alert(\'This is the new onclick event!\');' ; alert(rectelem.onclick); // shows that the change was made. When I switch back to the main page and click on the object though, the initial onclick behavior is gone but the new behavior does not take place. Nothing happens and I get no script error to look at. I've tried moving the new onclick behavior to a function and then setting the object's onclick to the address of that function: rectelem.onclick = test; function test() { alert('This is the test function!'); } This approach has had the same result so far :( Any ideas on what I'm doing wrong or how to change this events behavior at runtime?

            T Offline
            T Offline
            T Smooth
            wrote on last edited by
            #5

            I came up with the following solution: On the main page I have a function that takes a string and then executes that string as code using the eval function: function execCode(codestring) { eval(codestring); } Then on the popup page I do the following to change the onclick event's code: window.opener.execCode("window.document.getElementById('event2_2372').onclick = function() { alert('Modified event code!'); };"); This allows the "function()" assignment to take place in the address space of the main page thus giving it a valid entry point to the function. If anyone has any tweaks or a better way to do this, please let me know. Thanks!

            1 Reply Last reply
            0
            • T T Smooth

              This only works if that element is on the same page as the function. My element and function that I want to be used for the element's onclick event, reside on the main page but I need to modify that function's code from the popup page.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Yes, I did some testing, and it appears that the anonymous function still belongs to the page where it is created, although the function is not declared in the page.

              --- b { font-weight: normal; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups