473,804 Members | 3,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

js script loaded into div with ajax doesn't work

bugboy
160 New Member
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 4623
bugboy
160 New Member
Ok i'm going to attempt to post all the code
Nov 8 '08 #2
bugboy
160 New Member
ok finally managed to get all the code into the first post by editing it.
Nov 8 '08 #3
gits
5,390 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 New Member
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=funct ion(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 Recognized Expert Moderator Expert
?? 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 New Member
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
5068
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 example, the user make a choice by clicking on an item called 20050928, and as a result a file named "20050928/case.js" is "included", and the data contained within is available.
0
3232
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 converted/developed with VB.NET. What I want from debugging is to be able to step into the methods in the DLLs called from ASP scripts using Visual Studio .NET. Background: For typical script debugging issues, you can read and follow the two documents on...
17
34745
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 fine. However the HTML have a SCRIPT tag that the browser should process, but
13
4008
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, asynchronously. These tasks might take long time so I want to keep the user informed of the progress. The problem is that only the PHP script knows the progress, how can the web page gets these information from PHP script?
1
3958
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 my objective is this that when i uplod a file it should be desply on same page with ajax uplod after when i refresh page this should be not remains longer and on clicking other image its replase previous image plz help how i can do this the...
8
3993
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 display is a javascript image gallery. i am trying to do multiple galleries on one page. When the page loads the default tab loads the gallery correctly. But when I click on the next tab the javascript gallery fails. The script is exactly the same...
5
2626
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 but I've been experiencing something a little weird lately with regards to FireBug. I'm getting an error message: Now the interesting thing to note about this error is that the method "Sys$CultureInfo$_getAbbrMonthIndex" is part of the .NET...
7
4050
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. Before there are six characters entered in the CAPTCHA code field, the 'Send' button is supposed to be disabled. When there are at least six characters in the CAPTCHA code field, the script attempts to verify the CAPTCHA w/AJAX. If it verifies, it...
4
2292
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"> <textarea id="'.$id.'">'.$text.'</textarea> <script type="text/javascript">event_listener("'.$id.'");</script> </div>
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.