Solved

JQuery AJAX load and split at the same time

Posted on 2014-09-20
6
179 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
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!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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…

710 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