Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Generated JavaScript 1

Status
Not open for further replies.

portalguy123

Programmer
Joined
Sep 19, 2006
Messages
40
Location
US
Hello Experts,

I am working on an AJAX application and I get a response back from the url which populates a div which has both HTML as well as JavaScript code , I need to extract the javascript from the response and set in the parent documnet's head tag so as to be able to acees the javascript in a global context.I use the following code to do so ,

Code:
extractAndProcess = function(windowObj,n){

    var re = /<script[\s\S]*?>([\s\S]*?)<\/scr/igm;
    var result = '';
    var match; 
    while (match = re.exec(responseString)) { 
     //document.write(match[1]);
     //eval(match[1]);
     result += match[1]; 
     //alert ('The match is '+match[1]);
   }
   
   alert('The result is '+result);
    
   var head = document.getElementsByTagName("head")[0];
   script = document.createElement('script');
   script.id = 'uploadScript';
   script.type = 'text/javascript';
   [b]script.src = result;[/b]
   head.appendChild(script);
   
   alert('The set script is '+   document.getElementsByTagName("head")[0].value);
   populateEmailids(windowObj,n);
}

But when I print the value of teh head element I get a null back , can you please let me know where I might be wrong.

Thanks in Advance!!

-Bob F.
 
Anyone?? Flava and Trollacious not even you guys!!!
 
What does the HTML at the top of the page look like?

Lee
 
Hello Lee,

Thanks for the reply!Are you asking what the top of teh HTML looks after the head element is appended with a script element? I think the problem Iam facing is that the script element has a src attribute which can only point to a URL ,but in my case I have the script in a variable , and thats where I am stuck at, to have the dynamically created script element have this javascript in the variable as its content.I believe you cant use innerText and innerHTML for the script tag , is there any other way around..
 
Though its a AJAX scenario, its a general javascript issue, I wouldnt think it to be AJAX specific..
 
It's your word, not mine. Why then you cross post to there?
 
Hmm - hang on - what happens if you print the value of the head element BEFORE you try to append? If it's null, the append will never work.

You must be able to get a reference to the head element to be able to append to it.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hello Dan,

Thanks for the reply, I able to print the innerHTML of the head before appending the script element to teh head element.

I have tried multiple scenarios, all of them fail.Before I mention them , just to let you know the JS is in a string not in a file or filename.

Case 1: Try to append the head.innerHTML with <script>'js string' </script> --doesnt work get an error saying "Could not set the innerHTML property. Invalid target element for this operation".

Case 2: Try to create a script element and append it to the head element, I couldnt figure out a way to have the script in the string made part of the script element, script.src can only point to a url, script.innerText and script.innerHTML cant be used either they give out errors as well, I am using IE and the head element is not null as I am able to print the innerHTML of it.
 
Unfortunately ,it needs to work in IE at the least..
 
Now that it seems I cant have the script element populated with the script code in the string var , I have chnaged my approach to extract the java script and evaluate it , I get an error for the following regexp , can anyone please let me know what might be wrong with this RegExp.

Thanks in Adnavce!
Code:
var re2 = /\/\/ **** This section[\s\S]*?****([\s\S]*?)-->/igm;
 
I don't think you can have multiple * in a row. An * in regexp says match 0 or more instances of the preceding character, so it doesn't make any sense to have more than 1 in a row. If you meant it to match the literal character * then you must first escape it with a backslash (\)

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
I am trying to extract the java script in the following text...

Code:
// **** This section contains code that will run when the page is loaded ****


// Build the netui_names table to map the tagId attributes
// to the real id written into the HTML
if (netui_names == null)
   var netui_names = new Object();
netui_names.fname0="fname0"
netui_names.email0="email0"
netui_names.lname0="lname0"
netui_names.box0="wlw-checkbox_key:{request.results[0].checked}"
netui_names.mi0="mi0"

-->
 
I am hiting one roadblock after another after evaluating the script extracted in the above post , I dotn see the values in the netui_names object.I am using the following code to extract the scipt from the above example and evaluating them as follows but I dotn see the values in netui_names object..

Code:
var re2 = /\/\/ \*\*\*\* This section[\s\S]*?\*\*\*\*([\s\S]*?)-->/igm;
   var result2 = '';
   var match2 = '';
   while (match2 = re2.exec(result)) { 
      result2 += match2[1]; 
     
   }
   alert('The result2 is '+result2);
   eval(result2);

Can any body please let me know why I cant see the effect of eval on the netui_names object ..

FYI the result2 has the following script stored in it..

Code:
// Build the netui_names table to map the tagId attributes
// to the real id written into the HTML
if (netui_names == null)
   var netui_names = new Object();
netui_names.fname0="fname0"
netui_names.email0="email0"
netui_names.lname0="lname0"
netui_names.box0="wlw-checkbox_key:{request.results[0].checked}"
netui_names.mi0="mi0"

Is it becuase it has comments in the beginning...
 
Ok it seems that the problems is that the following code is not being evaluated ...

var result2 has the following value;and eval(result2) has not effect , can anyone shed some light on this aspect of the problem..

Thanks.

result2=
Code:
 // Build the netui_names table to map the tagId attributes
// to the real id written into the HTML
if (netui_names == null)
   var netui_names = new Object();
netui_names.fname0="fname0"
netui_names.email0="email0"
netui_names.lname0="lname0"
netui_names.box0="wlw-checkbox_key:{request.results[0].checked}"
netui_names.mi0="mi0"

-Bob.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top