Solved

JQuery AJAX load and split at the same time

Posted on 2014-09-20
6
180 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
[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
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
Industry Leaders: 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!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…
Suggested Courses

635 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