By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Executing script using innerHTML

P: 2
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.getElementById("content").innerHTML = http.responseText.
Problem i am facing is i got a javascript code present next to the textbox tag in change screen jsp pages
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.   window.document.forms[0].elements[0].focus
  3. </script>
  4.  
The above mentioned script is not getting executed .

Can anyone suggest me a solution for the above problem.

Thanks in advance

Regards
Das
Mar 12 '09 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,390
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
Mar 12 '09 #2

Frinavale
Expert Mod 5K+
P: 9,731
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:none;"

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


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
Mar 12 '09 #3

P: 2
Hi

Thanks for your response.

I tried to execute javascript using eval() , its working if i eval the http.responseText. script present in the http.responseText 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.responseText 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
Mar 16 '09 #4

acoder
Expert Mod 15k+
P: 16,027
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:
Expand|Select|Wrap|Line Numbers
  1. var script = document.createElement("script");
  2. script.type = ...
  3. script.src = ...
  4. document.getElementsByTagName("head")[0].appendChild(script);
Mar 17 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.