Executing script using innerHTML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karuppiahdas
    New Member
    • Mar 2009
    • 2

    Executing script using innerHTML

    Hi

    I am facing a problem in executig the javascript in innerHtml.

    I got a login screen(jsp page), with two form in the bodyl.One form is wrapped by a div tag.Which has got the login information like username password.
    The request is posted through ajax. On processing the request, if password is expired change password screen will get dispalyed.

    The content is getting displayed by replacing the div tag of login screen.
    Code :document.getEl ementById("cont ent").innerHTM L = http.responseTe xt.
    Problem i am facing is i got a javascript code present next to the textbox tag in change screen jsp pages
    Code:
    <script>
      window.document.forms[0].elements[0].focus
    </script>
    The above mentioned script is not getting executed .

    Can anyone suggest me a solution for the above problem.

    Thanks in advance

    Regards
    Das
    Last edited by gits; Mar 12 '09, 06:53 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    when you load javascript-code through an ajax-call and just insert it to the page it will not be evaluated ... this is just done during a real page-load and so you need to eval() it explcitly ...

    kind regards

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Instead of using JavaScript to change the InnerHTML of an element if the user's password has expired, consider redirecting the user to a new page.....called Change Password.

      Another way to do it would be to have one <form> tag on the page.

      Place the code that prompts the user for a password in a <div> that displays initially.
      If the Server Code determines that the user's password has expired, set the style of this <div> to "display:no ne;"

      You'll have to place the HTML that lets the user enter a new password in another <div> which has the style of "display:no ne" initially. If the Server Code determines that the user's password has expired, the server code changes the style of this <div> to "display:bl ock"


      The reason I'm advising against using JavaScript for this is because it is the server code that determines whether or not the user's password has expired....and so it should be server code that determines what is displayed.

      You could do this using JavaScript as well.....

      Again, place each section in it's own <div>. Write a JavaScript function that changes the style of the <div>'s during the <body>'s onload event. You will need to somehow indicate to the JavaScript function that the password has expired so that it is able to determine which <div> should be displayed.

      -Frinny

      Comment

      • karuppiahdas
        New Member
        • Mar 2009
        • 2

        #4
        Executing script using innerHTML

        Hi

        Thanks for your response.

        I tried to execute javascript using eval() , its working if i eval the http.responseTe xt. script present in the http.responseTe xt object gets executed but when it loads into the div tag the login page its not working .

        I am little bit confused with div attribute like display:none or diplay block ..

        Is there any way of declaring a new method which will separate the script tag from the http.responseTe xt and adding it to the end of the new page content.

        I found it from the blogs using DOM methods will be longlasting solution .

        Can u please help on that ..

        Thanks in advance .

        regards
        Das

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Since it's a simple line of JavaScript, why not just add it within the Ajax script when the request is complete and the change password form is shown?

          If, for some reason, you want to return JavaScript code, use a unique delimiter which you can split() on to separate the HTML from the JavaScript. Alternatively, you can parse the returned code, e.g. find the <script> tag.

          An alternative to eval is to use dynamic script tags:
          Code:
          var script = document.createElement("script");
          script.type = ...
          script.src = ...
          document.getElementsByTagName("head")[0].appendChild(script);

          Comment

          Working...