473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

js script loaded into div with ajax doesn't work

bugboy
160 100+
I have a page being generated with PHP, it includes a DIV who's content gets swapped with ajax. the new content contains the following function call PHP echo'd inside the parent DIV.

The code works fine when originally loaded in the DIV but not when it's been swapped there with ajax.

Should i be putting the function call somewhere else in the code? The $row_num needs to reflect the row number originally clicked to call the ajax function.

Ok i'm going to attempt to post the actually code.. the Bytes system always looses when i submit.. it cuts it off at the first close code tag. sorry it won't let me post the code.. keeps cutting it off...

here are the functions:
Expand|Select|Wrap|Line Numbers
  1. function el(tid) 
  2.     {
  3.     return document.getElementById(tid);
  4.     }
  5.  
  6.     el(\'focus_here\').onkeydown=function(e)
  7.                 { 
  8.                 e=e||window.event;
  9.                 var kc = e.keyCode; 
  10.                 if(kc==13)
  11.                     {
  12.                     new_row(\''.$row_num.'\')
  13.                     }
  14.                     else
  15.                     {
  16.                     resize_textarea(this, \''.$indent_compensate.'\')
  17.                     }
  18.                 }

Here is the new code to be added with ajax:

Expand|Select|Wrap|Line Numbers
  1. elseif($_SESSION['state'][$row_num]=='edit')
  2.                 {
  3.                 echo'<textarea id="focus_here" name="edit" rows="1" spellcheck="true" 
  4.                 onblur="save_edit_row(\''.$row_num.'\')" 
  5.                 onkeydown="resize_textarea(this, \''.$indent_compensate.'\')"  
  6.                 onfocus="resize_textarea(this, \''.$indent_compensate.'\')" '.
  7.                 $handle.'
  8.                 </textarea></div>';
  9.                 echo'<script type="text/javascript">
  10.  
  11.                 document.getElementById(\'focus_here\').focus(); 
  12.  
  13.                 el(\'focus_here\').onkeydown=function(e)
  14.                 { 
  15.                 e=e||window.event;
  16.                 var kc = e.keyCode; 
  17.                 if(kc==13)
  18.                     {
  19.                     new_row(\''.$row_num.'\')
  20.                     }
  21.                     else
  22.                     {
  23.                     resize_textarea(this, \''.$indent_compensate.'\')
  24.                     }
  25.                 }
  26.                 </script>';
  27.  
  28.                 }
Nov 8 '08 #1
9 4601
bugboy
160 100+
Ok i'm going to attempt to post all the code
Nov 8 '08 #2
bugboy
160 100+
ok finally managed to get all the code into the first post by editing it.
Nov 8 '08 #3
gits
5,390 Expert Mod 4TB
when you 'load' JavaScript-code with ajax then you have to eval() it explicitly since this is not similar to a 'normal' page-load and the code isn't evaled and you cannot use it.

kind regards
Nov 8 '08 #4
bugboy
160 100+
Cool! I've been trying everything..

Ok.. so I've read about it and tried eval()ing different things and i'm afraid i just don't get it.. it seems that it only needs to be used for some parts of the code.. but which ones.. any further help would be greatly appreciated!

here is one of many stabs at it..

Expand|Select|Wrap|Line Numbers
  1.                 echo'<script type="text/javascript">
  2.  
  3.                 document.getElementById(\'focus_here\').focus(); 
  4.  
  5.                 eval(el(\'focus_here\').onkeydown=function(e))
  6.                 { 
  7.                 eval(e=e||window.event);
  8.                 eval(var kc = e.keyCode); 
  9.                 eval(if(kc==13)
  10.                     {
  11.                     new_row(\''.$row_num.'\')
  12.                     }
  13.                     else
  14.                     {
  15.                     resize_textarea(this, \''.$indent_compensate.'\')
  16.                     }
  17.                 })
  18.  
  19.  
  20.                 </script>';
Nov 8 '08 #5
gits
5,390 Expert Mod 4TB
nope ... you need to have the JavaScript-code to be evaled at the client ... so you might echo it to a certain element like a hidden textarea. then get it in your callback from that div and eval the code ... i hope you get the basic idea?

tha callback is the function that is assigned to you request's onreadystate-handler.

kind regards
Nov 9 '08 #6
bugboy
160 100+
i'm so lost.. sorry... i though i understood how this all works but i'm getting muddled..

Does echoing it to an element mean putting it inside the element's tags? When does the call back happen.. are you talking about when the code if first sent to the page.. or when, in this case, the 'return' key is hit.. which would be the second ajax request.

Even just hearing this said in a different way may help, i'm self teaching so my understanding or lack of the vocabulary may be to blame. I Think i understand the principles just not clear the order or location of the actions needed..

arrrg
Nov 9 '08 #7
bugboy
160 100+
Ok i Got it... it was way simpler than i was making it.. i just put it into an onkeyup event and it worked duh.. i was trying to put i outside the textarea in <script> tags... it still confuses me because it's acting on the onkeydown=function(e) thing.. i'm learning by trial and error so i find solutions that i don't understand..

thanks for you help, much appreciated.


Expand|Select|Wrap|Line Numbers
  1. onkeyup="el(\'focus_here\').onkeydown=function(e)
  2.                 { 
  3.                 e=e||window.event;
  4.                 var kc = e.keyCode; 
  5.                 if(kc==13)
  6.                     {
  7.                     new_row(\''.$row_num.'\')
  8.                     }
  9.                     else
  10.                     {
  11.                     resize_textarea(this, \''.$indent_compensate.'\')
  12.                     }
  13.                 }" 
Nov 9 '08 #8
gits
5,390 Expert Mod 4TB
?? it is working now? the confusion is right ;) ... in case you have the same event-handler already before your update then it works with those one and not with your new one ... since the new one is not available since it isn't evaled in your page ...

basicly things follow the following schema:

- webpage is loaded
- html and javascript is parsed and evaled

-> page and code works

- you make an ajax-request and get html-code & javascript-code
- you add it to a div
- the html is rendered into the DOM
- the script is not parsed and evaled ... so this should be done explicitly ...

-> just seperate the js-code in any way like writing (echoing) it to a hidden element - then retrieve it from there and eval this 'text' that the script-code is in this moment ... that will make it available for use in the page ->

in case you don't need to change the handler ... then you could just leave out the js-code from the response ... this is the 'better' way ... the js-code typically shouldn't be loaded dynamically ... but this is just a rule of thumb that comes from my experience and someone could have another view on that matter ...

loading script-code dynamically makes it very difficult to maintain larger code-bases since the handling and code isn't well centralized and could be well debuged ... it would be better to centralize the scriptcode for a page and just update the content and not the behaviour ...

kind regards
Nov 9 '08 #9
bugboy
160 100+
Thanks so much, you've been a great help!

Kind regards

Bugboy
Nov 11 '08 #10

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

Similar topics

7
by: cjl | last post by:
Hey all: I've searched the newsgroup, and googled, but I'm stuck. I want to be able to 'dynamically' add a .js file to a web page after the page has loaded, based on user interaction. For...
0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
17
by: PJ | last post by:
Greetings... I have stumbled upon a small problem. I use Ajax to retrieve part of a page I need to update. I update a DIV element with the HTML contents I get from another page. It works...
13
by: Marvin Zhang | last post by:
Hi, I'm not familiar with web programming, but I have a problem here. I have a page. When a user click one button on it, I will use AJAX to request a PHP script which will do a bunch of tasks,...
1
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
8
by: jackrabbithanna | last post by:
I'm using a ajax script the uses a tabbed div that fetches a remote webpage to display for each tab.when clicked. the script works fine except for one thing. The content of the page i want to...
5
Frinavale
by: Frinavale | last post by:
I was just wondering where/how JavaScript is loaded? Is it loaded for the specific page or for the Browser? I would have thought it was just specific to the page that the JavaScript exists within...
7
by: mike57 | last post by:
The minimal AJAX script below works in Firefox, but not in IE, Opera, or Chrome. I could use some suggestions or referrals to resources that will help me get the script working in other browsers. ...
4
bugboy
by: bugboy | last post by:
I'm adding a script to a <div> element dynamically, after page load, via AJAX. My new script is just ignored. Ho do i get the script to execute when dynamically loaded? <div id="a"> ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.