Solved

Extract javascript block from ajax content and execute it

Posted on 2008-10-28
6
2,617 Views
Last Modified: 2012-05-05
HI,

I would like to extract a specific javascript block, which has an ID, from the content I received by making an ajax call and then execute that javascript block. Is it possible to do it using JQuery? Here's how I started out:
$(document).ready(function(){
           
   $.ajax({
            url: "Page2.aspx",
            type: "GET",
            success: function(content){
                    
                var scriptBlock = $(content).find('script#myscript');
                
                // Now execute the script block
            }
          });
});

Open in new window

0
Comment
Question by:wtor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 22828857
What is in your response, a javascript code or html?
If it is only javascript then put your code in a function and then after ajax call you will be able to call that function.

see the example

/* response */
<script>
function fnOne(){
 alert("Function one called");
}
function fnTwo(){
 alert("Function two called");
}
</script>
/* end response */
 
/* after ajax call */
 
success: function(content){
                    
        eval(content);      // make the script executable
        fnTwo();            // call to the function
            
         // Now execute the script block
   }

Open in new window

0
 

Author Comment

by:wtor
ID: 22828916
Sorry I forgot to mention that my response was the entire HTML of Page.aspx.
0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 22828931
Can you place your response html here?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wtor
ID: 22828945
Here's the HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" action="Page.aspx">
 
        <script id="myscript" type="text/javascript">
            
            function demoClass()
            {
                var myval;
            }
            
            var obj = new demoClass();
            obj.myval = 10;
            
            alert( "This is a script in page 2. Value: " + obj.myval );
        </script>
 
    </form>
</body>
</html>

Open in new window

0
 
LVL 16

Accepted Solution

by:
sh0e earned 250 total points
ID: 22828964

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $.ajax({
            url: "Page2.aspx",
            type: "GET",
            cache: false,
            dataType: 'xml',
            success: function(xml){
                    
                eval($('script#myscript',xml).text())
            }
          });
});
</script>

Open in new window

0
 

Author Comment

by:wtor
ID: 22829168
Thanks sh0e for pointing me to the right direction. I just had to change the dataType to html and it worked!
I dont like using eval. If you google, you'll know why. Here's the final code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $.ajax({
            url: "Page2.aspx",
            type: "GET",
            cache: false,
            dataType: 'html',
            success: function(html){
                    
            var myScript = new Function($('script#myscript',html).text());
 
myScript();
            }
          });
});
</script>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
REST call Failing 1 36
Set css in function 11 51
center div inside another div 2 29
Why is my combox not displaying in jqwidgets grid 14 31
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question