Solved

JQuery AJAX load and split at the same time

Posted on 2014-09-20
6
174 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

919 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

20 Experts available now in Live!

Get 1:1 Help Now