Solved

JQuery AJAX load and split at the same time

Posted on 2014-09-20
6
173 Views
Last Modified: 2014-09-21
Using Jquery I call a server side function to return some text by loading it into an ID as follows:

$('#someid').load('/lookup.asp?ID=' + ID);

Open in new window


All works fine. However the output of the server side function will be surrounded by special string delimiters and I would like to split the string so only the text within the delimiters is loaded.

So for example the output could be:

<p>Some start text</p>
### START ###
<p>The payload text.</p>
### END ###
<p>Some end text.</p>

Open in new window


But what I would like is to only load the following:

<p>The payload text.</p>

Open in new window

0
Comment
Question by:mike99c
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40334402
Something simple like

theresult=thestring.split("### START ###\n")[1].split("\n### END ###")[0];

Open in new window


Only problem is the string will be injected in to the element straight away, so you either have to use .ajax and format the response before inserting it or format the string as it appears in the someid element in a callback when received
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40334403
So parse it server side and only return what you want to display.  You could also parse id it clent side but the overhead would then negate part of the benefit you get by getting the data in background with AJAX.

Cd&
0
 

Author Comment

by:mike99c
ID: 40334410
I can't parse it server side as the rest of the output I want to strip out is in fact the HTML of the page. Which is why the server script outputs the text with the delimiters.

I am aware about string split functions but can I somehow attach it to the load function? If not how do I copy the server side output to a local variable?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40334415
$.ajax({
	url: '/lookup.asp?ID=' + ID,
	success: function(response) {
		theresult=response.split("### START ###\n")[1].split("\n### END ###")[0];
		$('#someid').html(theresult);
	}
});

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40335009
@Gary you pollute the global scope when you do not add var in front of local vars
0
 

Author Closing Comment

by:mike99c
ID: 40335215
Made a slight change:

$.ajax({
      url: '/lookup.asp?ID=' + ID,
      success: function(response) {
            var theresult=response.split("### START ###\n")[1].split("\n### END ###")[0];
            $('#someid').html(theresult);
      }
});
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now