开发者

Dynamically Creating Javascript? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I'm trying to make an AJAX oriented board and I want to handle each articles' written time fields and update those time fields every 5 seconds. I would like to show contents with changing written time every 5 seconds whether there is a new comment on an article when a user is rea开发者_运维问答ding it. So the key point is that handling DOM objects in page but the hard part is that typically javascript is read before the whole page is read but I would like to innerHTML changing (dynamically) javascript and also want to handle DOM objects(also dynamically changed and inserted through innerHTML) with it

Is there any way that I can put a dynamically created Javascript which is from PHP and insert that to the HTML by AJAX (jQuery) ?


Although I'm honestly confused by your question, I think your solution may be creating a looping ajax function that updates the various innerHTML's...so the last line in your ajax function would be: setTimeout(myAjaxFunction, 5000);


It would help a lot if you clarified your question. Structurally, javascript can be run at a number of different times in the life of a web page. Here are a few of those times:

  1. As the script is being loaded. This can be before, in the middle or after the page depending upon where the scripts are located in the page.
  2. After the page has been loaded. There are events that can tell you when the page has been loaded and it's safe for scripts to start modifying the page.
  3. Upon various events. The pages has lots of events such as clicks, mouse movements, keys pressed, scrolling, etc... that a javascript function can be attached to.
  4. Based on timers. You can create timers to run a function some time in the future or at some regular interval.

So, in your case, it sounds like you would want to do something like the following:

  1. Run a javascript function when the page was loaded that would set an interval timer for 5 seconds.
  2. Upon that interval timer, you would execute a function that would make an ajax call and establish a completion function for that ajax call.
  3. In the completion function for the ajax call, you would parse the contents received from the ajax call and then make corresponding changes to the local page using various DOM functions to modify the contents of the page.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜