473,440 Members | 1,640 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,440 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 4602
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"> ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.