Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

making multiple url calls on the same page in javascript

Posted on 2010-08-26
6
Medium Priority
?
376 Views
Last Modified: 2013-11-19
The easiest way to describe this is by example. If you look below I am try to implement the same piece of javascript code twice, but one uses a different search term. On my page though only the second one works properly:

http://www.glowfishtw.com/landing_pages/blog_search_example.html

How can I get both to run properly together?

Thanks


<html>
 <head>
     <script language="javascript">
         document.domain="construction.com";
     </script>
     <script type="text/javascript" src="http://sitelife.construction.com/ver1.0/Direct/DirectProxy"></script>

 </head>
 <body>

   <script language="javascript">
         document.domain="construction.com";
     </script>
     <script type="text/javascript" src="http://sitelife.construction.com/ver1.0/Direct/DirectProxy"></script>
 </head>
 <body>
   <script language="JavaScript">
       var requestBatch = new RequestBatch();
       var serverUrl = "http://sitelife.construction.com/ver1.0/Direct/Process";
   
       var searchType = "BlogPost";  // others are Comment, ForumPost, Gallery, Photo, Video
       
       // var searchString = "Title:Pennsylvania";  // To search only the title for Penn
       var searchString = "Pennsylvania";
              
       requestBatch.AddToRequest(new SearchAction(searchType, searchString, 10, 1));
       requestBatch.BeginRequest(serverUrl, clientCallBack);

       function clientCallBack(responseBatch) {
         // console.dir(responseBatch);  // -- use this in Firefox (only) to show the responseBatch object in the console to see what data is available.
         
         var searchResults = responseBatch.Responses[0].SearchResult.SearchResults;
         var htmlString = "<h2>" + searchType + " Search Results " + "for " + searchString + ":</h2><br>";

	 if (searchType == "BlogPost") // only showing an example of BlogPost for now
	 {
           for (var i = 0; i < searchResults.length; i++)
             htmlString += "<a href=\"" + searchResults[i].Url + "\">" + searchResults[i].PostTitle + "</a> by: <a href=\"" + searchResults[i].PostAuthor.PersonaUrl + "\">" + searchResults[i].PostAuthor.DisplayName + "</a> on " + searchResults[i].PostDate + "<br>";
         }

	 document.getElementById("output_div").innerHTML = htmlString;
       }

   </script>
   <div id="output_div"></div>
  
  
   <script language="JavaScript">
       var requestBatch = new RequestBatch();
       var serverUrl = "http://sitelife.construction.com/ver1.0/Direct/Process";
   
       var searchType = "ForumPost";  // others are Comment, ForumPost, Gallery, Photo, Video
       
       // var searchString = "Title:Pennsylvania";  // To search only the title for Penn
       var searchString = "Pennsylvania";
              
       requestBatch.AddToRequest(new SearchAction(searchType, searchString, 10, 1));
       requestBatch.BeginRequest(serverUrl, clientCallBack);

       function clientCallBack(responseBatch) {
         // console.dir(responseBatch);  // -- use this in Firefox (only) to show the responseBatch object in the console to see what data is available.
         
         var searchResults2 = responseBatch.Responses[0].SearchResult.SearchResults;
         var htmlString2 = "<h2>" + searchType + " Search Results " + "for " + searchString + ":</h2><br>";

	 if (searchType == "ForumPost") // only showing an example of BlogPost for now
	 {
           for (var i = 0; i < 10; i++)
             htmlString2 += "<a href=\"" + searchResults2[i].PostUrl + "\">" + searchResults2[i].PostTitle + "</a> by: <a href=\"" + searchResults2[i].LiteUser.PersonaUrl + "\">" + searchResults2[i].LiteUser.DisplayName + "</a> on " + searchResults2[i].LastUpdated + "<br>";
         }

	 document.getElementById("output_div2").innerHTML = htmlString2;
       }

   </script>
   <div id="output_div2"></div>
   

   
 </body>
</html>

Open in new window

0
Comment
Question by:elliottbenzle
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
Fr0zT earned 1000 total points
ID: 33533833
I'm not sure but the comment on lines 21 and 52 indicates the following are valid search terms:
Comment, ForumPost, Gallery, Photo, Video

In your second script you are using ForumPost which is valid, but in the first one you are using BlogPost which is not indicated as being vaild.  I'm basing this purly on the comment on lines 21 and 52.
0
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33533874
BlogPost is actually valid. This is from an example script where BlogPost was the original term.
0
 
LVL 11

Assisted Solution

by:Rajesh Dalmia
Rajesh Dalmia earned 1000 total points
ID: 33533877
may be possible that htmlString in 1st js does not have any value from the looping... try initialize with some value before the loop starts like

htmlString = "MyVal";
 if (searchType == "BlogPost") // only showing an example of BlogPost for now
       {
           for (var i = 0; i < searchResults.length; i++)
             htmlString += "<a href=\"" + searchResults[i].Url + "\">" + searchResults[i].PostTitle + "</a> by: <a href=\"" + searchResults[i].PostAuthor.PersonaUrl + "\">" + searchResults[i].PostAuthor.DisplayName + "</a> on " + searchResults[i].PostDate + "<br>";
         }


if this prints MyVal in div that means loop is not working....
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33533899
also there's a difference in for loop in 2 js

1st case you are using the length and 2nd case it's fixed 10...

for (var i = 0; i < searchResults.length; i++)
0
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33534085
The first part already has a defined part. That's why if you look at the page you see the title two times:

"ForumPost Search Results for Pennsylvania:


ForumPost Search Results for Pennsylvania:"

Oddly though the variable displays ForumPost for both, this line actually looks like this:

Searchtypevariable + " Search results for Pennsylvania"

I don't know why the value for both is coming out as "ForumPost"


Also I found that the problem is only there in IE
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 33535287
Thanks guys. The problem was repeating variables. I renamed them searchType2, etc. in the second version of the code and now everything is working.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

810 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